gpt4 book ai didi

.net - Entity Framework 4 错误 : Unable to update the EntitySet because it has a DefiningQuery

转载 作者:行者123 更新时间:2023-12-03 15:09:44 24 4
gpt4 key购买 nike

好的,这是场景。我有3张 table 。一个称为 aspnet_Users 一个称为 Categories 和一个称为 User_Categories 的链接表。 aspnet_Users 和 Categories 都有主键(分别是 UserId 和 ID)。链接表只有两列:CategoryID 和 UserId,每列都设置了外键关系,并且我为 User_Categories 上的两列设置了唯一键。这在 aspnet_Users 表和 Categories 表之间建立了多对多关系。我从这个数据库设置中生成了我的实体 edmx 文件,一切看起来都很完美,几乎适用于所有操作。

我想要做的是从表单中添加一个新类别(它本身可以完美工作),同时,将特定用户与新提交的类别相关联。当我尝试这样做时,我的主题行中出现错误。这是我用来尝试此操作的代码(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");
}

为什么这不起作用?

最佳答案

我假设完整的异常消息类似于:

Unable to update the EntitySet YourTableName because it has a DefiningQuery and no InsertFunction element exists in the ModificationFunctionMapping element to support the current operation.



如果您的数据库表没有定义主键,就会发生这种情况。

将主键添加到您的表中(使用 SQL Server Management Studio 或其他工具),并更新您的 .edmx数据库中的模型。

关于.net - Entity Framework 4 错误 : Unable to update the EntitySet because it has a DefiningQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com