asp.net-mvc 尝试更新实体框架中的模型时,为什么会出现“无法更新 EntitySet,因为它具有 DefiningQuery...”异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6774871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:20:11  来源:igfitidea点击:

Why am I getting a "Unable to update the EntitySet because it has a DefiningQuery..." exception when trying to update a model in Entity Framework?

asp.net-mvcentity-frameworklinq-to-sqlexception

提问by Thomas Mathew

While updating with the help of LINQ to SQL using Entity Framework, an exception is thrown.

使用实体框架在 LINQ to SQL 的帮助下进行更新时,抛出异常。

System.Data.UpdateException: Unable to update the EntitySet 't_emp' because it has 
a DefiningQuery and no <UpdateFunction> element exists in the   
<ModificationFunctionMapping>

The code for update is :

更新代码是:

public void Updateall()
    {
        try
        {


            var tb = (from p in _te.t_emp
                      where p.id == "1"
                      select p).FirstOrDefault();
            tb.ename = "Hyman";

            _te.ApplyPropertyChanges(tb.EntityKey.EntitySetName, tb);
            _te.SaveChanges(true);
        }
        catch(Exception e)
        {

        }
    }

Why am I getting this error?

为什么我收到这个错误?

回答by Thomas Mathew

The problem was in the table structure. To avoid the error we have to make one primary keyin the table. After that, update the edmx. The problem will be fixed

问题出在表结构上。为了避免错误,我们必须在表中创建一个主键。之后,更新edmx。问题将被修复

回答by George Stocker

Three things:

三件事:

  1. Don't catch exceptions you can't handle. You're catching every exception possible, and then doing nothing with it (except swallowing it). That's a Bad Thing™ Do you really want to silently do nothing if anythinggoes wrong? That leads to corrupted state that's hard to debug. Not good.

  2. Linq to SQL is an ORM, as is Entity Framework. You may be using LINQ to update the objects, but you're not using Linq to SQL, you're using Entity Framework (Linq to Entities).

  3. Have you tried the solution outlined here? The exception you posted is somewhat cut off, so I can't be sure it's exactly the same (please update your post if it isn't), and if it is the same, can you comment on whether or not the following works for you?

  1. 不要捕捉您无法处理的异常。你捕捉到所有可能的异常,然后什么都不做(除了吞下它)。这是一件坏事™ 如果出现任何问题,您真的想默默地什么都不做吗?这会导致难以调试的损坏状态。不好。

  2. Linq to SQL 是一个 ORM,实体框架也是。您可能正在使用 LINQ 来更新对象,但您没有使用 Linq to SQL,而是使用了实体框架(Linq to Entities)。

  3. 您是否尝试过此处概述的解决方案?您发布的例外情况有些中断,因此我无法确定它是否完全相同(如果不是,请更新您的帖子),如果相同,您能否评论以下内容是否适用你?

"[..] Entity Framework doesn't know whether a given view is updatable or not, so it adds the <DefiningQuery>element in order to safeguard against having the framework attempt to generate queries against a non-updatable view.

If your view is updatable you can simply remove the <DefiningQuery>element from the EntitySet definition for your view inside of the StorageModel section of your .edmx, and the normal update processing will work as with any other table.

If your view is not updatable, you will have to provide the update logic yourself through a "Modification Function Mapping". The Modification Function Mapping calls a function defined in the StorageModel section of your .edmx. That Function may contain the name and arguments to a stored procedure in your database, or you can use a "defining command" in order to write the insert, update, or delete statement directly in the function definition within the StorageModelsection of your .edmx." (Emphasis mine, post formatted for clarity and for Stack Overflow)

“[..] 实体框架不知道给定的视图是否可更新,因此它添加了<DefiningQuery>元素以防止框架尝试针对不可更新的视图生成查询。

如果您的视图是可更新的,您可以简单地<DefiningQuery>从 .edmx 的 StorageModel 部分内的视图的 EntitySet 定义中删除该元素,并且正常的更新处理将与任何其他表一样工作。

如果您的视图不可更新,您将必须通过“修改功能映射”自己提供更新逻辑。修改函数映射调用在 .edmx 的 StorageModel 部分中定义的函数。该功能可以包含名称和参数的存储过程在数据库中,或者您可以使用“定义命令”为了写在内部函数定义插入,更新,或直接delete语句 StorageModel的部分.edmx。” (强调我的,为了清晰和堆栈溢出而格式化的帖子)

(Source: "Mike" on MSDN)

(来源:MSDN上的“Mike” )

回答by porya_ras

But You can Set primary Key in Model if use MVC Asp.net

但是如果使用MVC Asp.net,您可以在模型中设置主键

Just Open model.edmx in your table ,go to your field property and set Entity Key = True

只需在您的表中打开 model.edmx ,转到您的字段属性并设置 Entity Key = True