c# - 无法更新 EntitySet '...' 因为它有一个 DefiningQuery

标签 c# asp.net-mvc entity-framework

我将 MVC 4 和 C# 4.5 与 EntityFramework 4 结合使用。

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

tblAdminUser -> tblAdminUserRole <- tblAdminRole

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

"Unable to update the EntitySet 'tblAdminUserRole' because it has a DefiningQuery and no element exists in the element to support the current operation."

我使用的代码是:

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

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

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

最佳答案

更新你的数据库,将一对外键设置为m2m表中的表作为主键。 然后将模型更新到数据库。

关于c# - 无法更新 EntitySet '...' 因为它有一个 DefiningQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12316711/

相关文章:

c# - Nullable<int> 是 "Predefined value type"- 或者 Equals() 和 == 在这里如何工作?

c# - ViewBag 属性在单元测试中始终返回 null

asp.net-mvc - 与 jquery 选项卡一起使用时,Html.Actionlink 显示错误的 Controller 名称

c# - TreeView 和 Entity Framework 绑定(bind)

entity-framework - EF Code First 给出错误:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'People' 中插入标识列的显式值。

c# - 如何让 Entity Framework 6 在 CSDL 中使用 SQL STUFF 函数?

c# - 修改打印机特定配置对话框的属性

c# - 脚手架和显示/编辑器模板

c# - WPF ListView ItemTemplate 内的文本框绑定(bind)

asp.net-mvc - WebAPI + 表单例份验证 + SimpleMembershipProvider 在不应该使用 WindowsIdentity 时使用