EntityFramework.Docs icon indicating copy to clipboard operation
EntityFramework.Docs copied to clipboard

many-to-many documentation here doesn't seem to work.

Open jsangco opened this issue 4 years ago • 2 comments

Could be something I'm missing of course, but I've tried several times following along with the code examples and the many-to-many relationship code doesn't seem to work.


Document Details

⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

jsangco avatar Sep 23 '22 21:09 jsangco

@jsangco What, specifically, doesn't work?

ajcvickers avatar Sep 26 '22 17:09 ajcvickers

Follow the example code in the provided documentation. The many-to-many relationship does not work.

After spending quite a bit of time debugging it, I figured out that it is due to the fact that EFCore does not pluralize its DbSet names anymore. This is problematic for people using a DB first method expecting the singular table names to map to pluralized proprieties.

Additionally, there's just generally poor consistency with table and property names in the sample code; thus leading to confusion. For example, the document has a table definition like this:

CREATE TABLE [PostTag] (
    [PostsId] int NOT NULL,
    [TagsId] nvarchar(450) NOT NULL,
    CONSTRAINT [PK_PostTag] PRIMARY KEY ([PostsId], [TagsId]),
    CONSTRAINT [FK_PostTag_Posts_PostsId] FOREIGN KEY ([PostsId]) REFERENCES [Posts] ([PostId]) ON DELETE CASCADE,
    CONSTRAINT [FK_PostTag_Tags_TagsId] FOREIGN KEY ([TagsId]) REFERENCES [Tags] ([TagId]) ON DELETE CASCADE
);

Then later has code like this (Please note the additional s in the name)

j.ToTable("PostTags"));

I would recommend going through the sample code for the many-to-many relationship and ensuring it can be used to execute working code (for both code-first and db-first if possible).

jsangco avatar Sep 30 '22 22:09 jsangco