.net 实体框架 4 错误:无法更新 EntitySet,因为它具有 DefiningQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3471331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework 4 Error: Unable to update the EntitySet because it has a DefiningQuery
提问by Tyler
Okay, here's the scenario. I have 3 tables. One called aspnet_Users one called Categories and a linking table called User_Categories. aspnet_Users and Categories both have primary keys (UserId and ID respectively). The linking table has only two columns: CategoryID and UserId, and there are foreign key relationships setup for each column AND I have a unique key setup for the two columns on User_Categories. This sets up a many-to-many relationship between the aspnet_Users table and the Categories table. I generated my entities edmx file from this database setup, and everything looks perfect and works for almost all operations.
好的,这是场景。我有3张桌子。一个称为 aspnet_Users 一个称为 Categories 和一个称为 User_Categories 的链接表。aspnet_Users 和 Categories 都有主键(分别是 UserId 和 ID)。链接表只有两列:CategoryID 和 UserId,每列都有外键关系设置,我为 User_Categories 上的两列设置了唯一键。这在 aspnet_Users 表和 Categories 表之间建立了多对多关系。我从这个数据库设置中生成了我的实体 edmx 文件,一切看起来都很完美,几乎适用于所有操作。
What I want to do is add a new category from a form (which works perfecly by itself), and also, at the same time, associate a specific user with that newly submitted category. When I try to do this I get the error in my subject line. Here is the code I'm using to try this (ctx is my entities context object):
我想要做的是从表单中添加一个新类别(它本身完美地工作),同时,将特定用户与新提交的类别相关联。当我尝试这样做时,我的主题行中出现错误。这是我用来尝试此操作的代码(ctx 是我的实体上下文对象):
public ActionResult Create(Category category, Guid userId)
{
aspnet_Users user = ctx.aspnet_Users.SingleOrDefault(x => x.UserId == userId);
ctx.Categories.AddObject(category);
user.Categories.Add(category);
ctx.SaveChanges();;
return RedirectToAction("Index");
}
Why doesn't this work?
为什么这不起作用?
回答by Drew Noakes
I assume the full exception message is something similar to:
我假设完整的异常消息类似于:
Unable to update the EntitySet YourTableNamebecause it has a DefiningQuery and no InsertFunction element exists in the ModificationFunctionMapping element to support the current operation.
无法更新 EntitySet YourTableName,因为它具有 DefiningQuery 并且 ModificationFunctionMapping 元素中不存在 InsertFunction 元素来支持当前操作。
This will occur if your DB's table doesn't have a primary key defined.
如果您的数据库表没有定义主键,就会发生这种情况。
Add a primary key to your table (using SQL Server Management Studio or whatever), and update your .edmxmodel from the database.
将主键添加到您的表(使用 SQL Server Management Studio 或其他工具),并.edmx从数据库更新您的模型。
回答by Moises Ribeiro
Error: Unable to update the EntitySet because it has a DefiningQuery
错误:无法更新 EntitySet,因为它有一个 DefiningQuery
Twice I got this error, twice I searched for answers everywhere, and in the end my mappings was messed up or the database had no primary key or something. I suggest checking it all out...
我两次遇到这个错误,两次我到处寻找答案,最后我的映射搞砸了或者数据库没有主键什么的。建议全部检查...
回答by AechoLiu
I encounter the same problem, and I fixed by the following link:
我遇到了同样的问题,我通过以下链接修复:
I remove the section of DefiningQueryin .edmx based on the content of 3rd link. And then everything works as a charm.
我根据第三个链接的内容删除了.edmx 中的DefiningQuery部分。然后一切都像魅力一样。
回答by Dan
Put a composite primary key on the mapping table, this will tell EF to handle it correctly.
在映射表上放置一个复合主键,这将告诉 EF 正确处理它。

