3

我将 MVC 4 和 C# 4.5 与 EntityFramework 4 一起使用。

我设置了一个简单的多对多表:

tblAdminUser -> tblAdminUserRole <- tblAdminRole

当我尝试向管理员用户添加角色时,出现以下错误:

“无法更新 EntitySet 'tblAdminUserRole',因为它有一个 DefiningQuery 并且元素中不存在支持当前操作的元素。”

我正在使用的代码是:

this.Role = new tblAdminRole()
{
    Name = "__role__",
};

context.tblAdminRoles.Add(this.Role);
context.SaveChanges();

this.AdminUser.tblAdminRoles.Add(this.Role);
context.SaveChanges();
4

2 回答 2

7

Update your database, set pair of foreign key to tables in m2m table as primary key. Then update your model to database.

于 2012-09-07T11:01:49.507 回答
4

Please check, if all ID fields in tblAdminUserRole have been set as Primary Key in your SQL database. Then update your model.

If Entity Framework can't figure out the primary key, it will generate a SELECT statement but it won't be able to create the according INSERT, UPDATE and DELETE statements.

于 2012-09-07T11:01:02.653 回答