3 ways for many in the Entity Framework

I have 3 data tables and a fourth table that displays between them, i.e. 3 id with all three columns as primary key.

I really want object A to contain a list, where B is an object containing a list. I would agree to a two-dimensional array of B and C in A.

Is there any way to do this? Am I really doing all this wrong?

+3
source share
2 answers

In the typical many-to-many case, where the connection table has only a composite key (two primary key fields), the Entity Framework will add navigation properties directly to the related objects. However, when there are additional fields in the join table, the Entity Framework creates an object for the join table so that you can "get" additional information.

In the case described above, the Entity Framework will generate Entity for your "join" table, which will have associations with all three of your objects.

I created an example using the objects Item1, Item2, Item3 and Link.

From a database perspective, if I have an Item1 identifier, I can query a table with something like:

select Item1ID, Item2ID, Item3ID from Link where Item1ID = 1

Item2, Item3 "", Item1 # 1. Linq to Entities, EF :

var q =
    from link in context.Link
    where link.Item1ID == 1
    select new { link.Item1, link.Item2, link.Item3 };

Item1, :

item1.Link.Select(l => new { l.Item1, l.Item2, l.Item3 });

, LINQ IQueryable / .

, . , !

+1

, . , - , .

Book < - many-to-many β†’ Author < - many-to-many β†’ Publisher < - Book

, .

3 3 :

a) ) a) Object Publisher

. , b) , " " " ". ,

[] β†’ [] β†’

[] β†’ [] β†’ [] β†’ [] β†’

, .

0

Source: https://habr.com/ru/post/1722475/


All Articles