C# 因为它有一个 DefiningQuery 并且 <ModificationFunctionMapping> 元素中不存在 <InsertFunction> 元素

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

because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element

c#.netentity-framework

提问by Elad Benda

I'm using EF in my application.

我在我的应用程序中使用 EF。

I try to save\insert a new record to a mapping table

我尝试保存\插入新记录到映射表

and get the following error:

并得到以下错误:

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

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

Should I define it in the edmx myself? How?

我应该自己在 edmx 中定义它吗?如何?

采纳答案by Elad Benda

My many-to-many mapping table was missing PK

我的多对多映射表缺少 PK

added, and the issue is solved.

添加了,问题就解决了。

回答by Biki

Agreed with the accepted answer. Just providing the reason behind it...

同意接受的答案。只是提供背后的原因......

When EF mapping is done with a table which does not have a primary key, it is treated as a view. Since views are logical entities, they can't be updated.

当对没有主键的表进行 EF 映射时,它被视为一个视图。由于视图是逻辑实体,因此无法更新。

So either add the missing primary key to your table or consider them as a view & don't perform any update operation on them.

因此,要么将缺少的主键添加到您的表中,要么将它们视为视图,不要对它们执行任何更新操作。

回答by kavitha Reddy

If your view is updatable you can simply remove the 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.

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

This is the case for me. Simply removing resulted in another error. I followed the steps of this post except the last one. For your convenience, I copied the 4 steps from the post that I followed to solve the problem as following:

这就是我的情况。简单地删除会导致另一个错误。除了最后一个之外,我遵循了这篇文章的步骤。为了您的方便,我从我遵循的帖子中复制了4个步骤来解决问题,如下所示:

  1. Right click on the edmx file, select Open with, XML editor
  2. Locate the entity in the edmx:StorageModels element
  3. Remove the DefiningQueryentirely
  4. Rename the store:Schema="dbo" to Schema="dbo" (otherwise, the code will generate an error saying the name is invalid)
  1. 右键单击 edmx 文件,选择打开方式,XML 编辑器
  2. 在 edmx:StorageModels 元素中找到实体
  3. DefiningQuery完全 删除
  4. 将 store:Schema="dbo" 重命名为 Schema="dbo"(否则代码会报错说名称无效)

回答by user3452580

  1. Right click on the edmx file, select Open with, XML editor

  2. Locate the entity in the edmx:StorageModels element

  3. Remove the DefiningQuery entirely

  4. Rename the store:Schema="dbo"to Schema="dbo"(otherwise, the code will generate an error saying the name is invalid) These steps worked for me

  1. 右键单击 edmx 文件,选择打开方式,XML 编辑器

  2. 在 edmx:StorageModels 元素中找到实体

  3. 完全删除 DefiningQuery

  4. 重命名store:Schema="dbo"Schema="dbo"(否则,代码将生成错误,指出名称无效)这些步骤对我有用

回答by Duncan Kuffar

Make sure your foreign key tables do not have more than one column names that match the columns in your referring table; only primary key should match between your ref table with refereed table. 0..1 should have distinguished column name, develop naming convention for your columns that will ensure referring and refereed tables do not have more than one matching columns names. Courtesy of ALMwConsult.net

确保您的外键表没有超过一个与您的引用表中的列相匹配的列名;只有主键应该在您的 ref 表与引用表之间匹配。0..1 应该具有可分辨的列名,为您的列制定命名约定,以确保引用和引用的表不会有多个匹配的列名。由 ALMwConsult.net 提供

回答by Moji

**Recheck your tables doesn't have PK :** just add Primary Key and then remove and re-add table to edmx then it should work

**重新检查您的表没有 PK :** 只需添加主键,然后将表删除并重新添加到 edmx 中,然后它应该可以工作

回答by bajran

If you are not handling primary key in your table then this issue will definitely occur in MVC while updating and inserting record database

如果您没有处理表中的主键,那么在 MVC 中更新和插入记录数据库时肯定会出现此问题

DbUpdate Exception was unhandled by user

用户未处理 DbUpdate 异常

回答by wctiger

I came across this problem on an updatable view. Found this articlesolved my problem.

我在可更新的视图中遇到了这个问题。发现这篇文章解决了我的问题。

So the underlying table of my view does have primary key defined but EF does not know which columns are in the PK as the entity was built on the view. The trick here is to 'convince' EF that your view can be updated as a table. Steps are almost the same as mentioned by above answers:

因此,我的视图的基础表确实定义了主键,但 EF 不知道 PK 中的哪些列,因为实体是在视图上构建的。这里的技巧是“说服”EF,您的视图可以更新为表格。步骤与上述答案中提到的几乎相同:

  1. Right click on the edmx file, select Open with, XML editor
  2. Locate the entity in the edmx:StorageModels element
  3. Remove the <DefiningQuery>section entirely
  4. Rename the store:Schema="dbo" to Schema="dbo"
  5. Change store:Type="Views"to store:Type="Tables"
  1. 右键单击 edmx 文件,选择打开方式,XML 编辑器
  2. 在 edmx:StorageModels 元素中找到实体
  3. <DefiningQuery>完全删除该部分
  4. 将 store:Schema="dbo" 重命名为 Schema="dbo"
  5. 更改store:Type="Views"store:Type="Tables"

回答by Hafte Nigus

Working on db first mvc application. The problem was i forget to define primary key for the table. Solved by adding primary key using alter command and updating the DAL(edmx)

处理 db first mvc 应用程序。问题是我忘记为表定义主键。通过使用 alter 命令添加主键并更新 DAL(edmx) 解决

回答by Rajdeep

My table didn't have primary key in sql table design. Added it and solved.

我的表在 sql 表设计中没有主键。添加并解决。