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

C#.NetEntity Framework

C# Problem Overview


I'm using EF in my application.

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.

Should I define it in the edmx myself? How?

C# Solutions


Solution 1 - C#

My many-to-many mapping table was missing PK

added, and the issue is solved.

Solution 2 - C#

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.

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

Solution 3 - C#

> 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.

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:

  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)

Solution 4 - C#

I came across this problem on an updatable view. Found this article solved 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:

  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"

Solution 5 - C#

  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

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

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

Solution 9 - C#

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

DbUpdate Exception was unhandled by user

Solution 10 - C#

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElad BendaView Question on Stackoverflow
Solution 1 - C#Elad BendaView Answer on Stackoverflow
Solution 2 - C#BikiView Answer on Stackoverflow
Solution 3 - C#kavitha ReddyView Answer on Stackoverflow
Solution 4 - C#wctigerView Answer on Stackoverflow
Solution 5 - C#user3452580View Answer on Stackoverflow
Solution 6 - C#RajdeepView Answer on Stackoverflow
Solution 7 - C#Duncan KuffarView Answer on Stackoverflow
Solution 8 - C#MojiView Answer on Stackoverflow
Solution 9 - C#bajranView Answer on Stackoverflow
Solution 10 - C#Hafte NigusView Answer on Stackoverflow