多对多关系插入和删除

时间:2022-10-04 10:44:40

I have entities

我有实体

  • user:

id

name

navigation :group

  • group:

id

name

navigation :user

relation:

  • usrgrp

iduser

idgroup

Here I wan't to add data to relation and not the main entities. Please give me example how to delete and insert into usrgrp.

在这里,我不想将数据添加到关系而不是主要实体。请举例说明如何删除并插入usrgrp。

Thanks in advance. s.

提前致谢。秒。

2 个解决方案

#1


The Entity Framework is going to subsume the usrgrp table into the navigation. There will not be an entity for it. Therefore, all access to this table is via the User and Group entities only. Since this is a many to many relationship, you can access the relationship from either end in much the same way.

实体框架将usrgrp表包含在导航中。它不会有实体。因此,对此表的所有访问仅通过用户和组实体。由于这是多对多的关系,因此您可以以相同的方式从任一端访问关系。

someUser.Group.Add(new Group());
someUser.Group.Remove(someExistingGroup);
context.SaveChanges();

#2


Craig is right because the usrgrp table has only 2 FKs, it is a pure join table, and as such the Entity Framework interprets this as a ManyToMany association.

Craig是对的,因为usrgrp表只有2个FK,它是一个纯连接表,因此实体框架将其解释为ManyToMany关联。

As the code samples Craig has above illustrate this is usually fine for most customers.

正如上面的代码示例Craig所说,这通常适用于大多数客户。

If however you want to independently manage the UserGroup relationship one option is to add an extra column to the usrgrp table, an Identity Column is probably easiest. The Entity Framework can then no longer collapse this table into just navigations, so you will see an Entity for the usrgrp table that you can manipulate independently.

但是,如果您想要独立管理UserGroup关系,一个选项是向usrgrp表添加一个额外的列,标识列可能是最简单的。然后,实体框架不再将此表折叠为导航,因此您将看到可以独立操作的usrgrp表的实体。

i.e.

someUser.UserGroups.Add(new UserGroup {User = someUser, Group = someGroup});

Notice you don't have to specify the Identity column (which would be a pain) because it is store generated.

请注意,您不必指定Identity列(这会很麻烦),因为它是存储生成的。

Generally most customers prefer not to see an Entity for pure join tables, because the programming experience is simplier without it. For example LINQ queries become a little more complicated:

通常,大多数客户不希望看到纯连接表的实体,因为没有它,编程体验会更简单。例如,LINQ查询变得有点复杂:

this:

var users = from g in ctx.Groups
            from u in g.Users
            where g.Name == "Administrators"
            select u;

becomes:

var users = from g in ctx.Groups
            from ug in g.UserGroups
            where g.Name == "Administrators"
            select ug.User;

Hope this extra clarification helps

希望这个额外的澄清有所帮助

Alex

#1


The Entity Framework is going to subsume the usrgrp table into the navigation. There will not be an entity for it. Therefore, all access to this table is via the User and Group entities only. Since this is a many to many relationship, you can access the relationship from either end in much the same way.

实体框架将usrgrp表包含在导航中。它不会有实体。因此,对此表的所有访问仅通过用户和组实体。由于这是多对多的关系,因此您可以以相同的方式从任一端访问关系。

someUser.Group.Add(new Group());
someUser.Group.Remove(someExistingGroup);
context.SaveChanges();

#2


Craig is right because the usrgrp table has only 2 FKs, it is a pure join table, and as such the Entity Framework interprets this as a ManyToMany association.

Craig是对的,因为usrgrp表只有2个FK,它是一个纯连接表,因此实体框架将其解释为ManyToMany关联。

As the code samples Craig has above illustrate this is usually fine for most customers.

正如上面的代码示例Craig所说,这通常适用于大多数客户。

If however you want to independently manage the UserGroup relationship one option is to add an extra column to the usrgrp table, an Identity Column is probably easiest. The Entity Framework can then no longer collapse this table into just navigations, so you will see an Entity for the usrgrp table that you can manipulate independently.

但是,如果您想要独立管理UserGroup关系,一个选项是向usrgrp表添加一个额外的列,标识列可能是最简单的。然后,实体框架不再将此表折叠为导航,因此您将看到可以独立操作的usrgrp表的实体。

i.e.

someUser.UserGroups.Add(new UserGroup {User = someUser, Group = someGroup});

Notice you don't have to specify the Identity column (which would be a pain) because it is store generated.

请注意,您不必指定Identity列(这会很麻烦),因为它是存储生成的。

Generally most customers prefer not to see an Entity for pure join tables, because the programming experience is simplier without it. For example LINQ queries become a little more complicated:

通常,大多数客户不希望看到纯连接表的实体,因为没有它,编程体验会更简单。例如,LINQ查询变得有点复杂:

this:

var users = from g in ctx.Groups
            from u in g.Users
            where g.Name == "Administrators"
            select u;

becomes:

var users = from g in ctx.Groups
            from ug in g.UserGroups
            where g.Name == "Administrators"
            select ug.User;

Hope this extra clarification helps

希望这个额外的澄清有所帮助

Alex