无法保存多对多关系

发布于 2024-11-16 02:00:32 字数 723 浏览 3 评论 0原文

我有以下表/视图

Users (View)
-> UserId
-> Name

Roles (Table)
-> RoleId
-> Name

UserRoles (Table)
-> UserId
-> RoleId

以及类

public class Role{
     public int RoleId{get;set}
     public string Name{get;set}
}

public class User{
     public int UserId{get;set}
     public string Name{get;set}
     public ICollection<Role> Roles{get;set}
}

和保存方法

using (var helper = new DbContext())
{
     helper.Users.Attach(user);
     helper.SaveChanges();
}

正如您在上面看到的,Users 是一个视图,UserRoles 是一个映射表。我能够检索用户实体以及映射的角色。但是在保存时它不会抛出任何异常,也不会保存。我尝试使用探查器检查数据库,但它甚至没有命中数据库。

由于用户是一个视图,我不想保存用户实体,而只想保存在角色集合中所做的更改。

I have the following tables/views

Users (View)
-> UserId
-> Name

Roles (Table)
-> RoleId
-> Name

UserRoles (Table)
-> UserId
-> RoleId

and the classes

public class Role{
     public int RoleId{get;set}
     public string Name{get;set}
}

public class User{
     public int UserId{get;set}
     public string Name{get;set}
     public ICollection<Role> Roles{get;set}
}

and the save method

using (var helper = new DbContext())
{
     helper.Users.Attach(user);
     helper.SaveChanges();
}

As you can see above, the Users is a view and UserRoles is a mapping table. I am able to retrieve User entities along with the mapped Roles. But while saving it is not throwing any exceptions nor is it saving. I tried checking the db using profiler and it is not even hitting the db.

Since Users is a view I don't want to save the User entity but only the changes made in the Roles collection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-11-23 02:00:32

这无法拯救任何东西。 Attach 将整个对象图放入上下文中,但处于状态Unchanged。如果上下文中的所有对象均未更改,SaveChanges 将不会向数据库发出任何命令(因为对于 EF 而言,没有任何更改)。

如果您想要进行 EF 识别的更改,您必须首先附加代表数据库中状态的对象,然后进行更改,例如:

using (var helper = new DbContext())
{
    helper.Users.Attach(user);
    helper.Roles.Attach(myNewRole);
    user.Name = myNewName;
    user.Roles.Add(myNewRole);
    // etc.
    helper.SaveChanges();
}

或者您可以将用户标记为已修改:

helper.Entry(user).State = EntityState.Modified;

但我相信这只会影响标量属性实体的角色,并不能解决向用户添加新角色的问题。

This cannot save anything. Attach puts the whole object graph into the context, but in state Unchanged. If all objects in the context are unchanged SaveChanges won't issue any command to the DB (because for EF nothing has changed).

If you want to make changes which EF recognizes as such you must first attach the object which represents the state in the Db and then make your changes, something like:

using (var helper = new DbContext())
{
    helper.Users.Attach(user);
    helper.Roles.Attach(myNewRole);
    user.Name = myNewName;
    user.Roles.Add(myNewRole);
    // etc.
    helper.SaveChanges();
}

Alternatively you can mark the user as modified:

helper.Entry(user).State = EntityState.Modified;

But I believe this only affects scalar properties of the entity and it doesn't solve the problem to add a new role to the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文