64

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?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • please read this post. https://stackoverflow.com/questions/7583770/unable-to-update-the-entityset-because-it-has-a-definingquery-and-no-updatefu – Emam May 13 '19 at 05:33

10 Answers10

109

My many-to-many mapping table was missing PK

added, and the issue is solved.

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 2
    I have the same problem, but I am not sure which column to select as primary key, because in all articles it is said that mapping table has to be table with just two foreign keys, adding some columns just ruins this many-to-many relationship. I have for example two tables Projects and Clients and Project_Client table where are two columns with foreign keys one to ProjectId second on ClientId. So where i have to set primary key? – GeekyNuns Aug 02 '16 at 13:41
  • Yes, a bit delayed as a comment, but this answer and the following helped me out. https://stackoverflow.com/a/217952/2850898 – Esaith Mar 01 '19 at 08:50
50

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.

tster
  • 17,883
  • 5
  • 53
  • 72
Biki
  • 2,518
  • 8
  • 39
  • 53
28

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)
Mahesh
  • 3,727
  • 1
  • 39
  • 49
kavitha Reddy
  • 3,303
  • 24
  • 14
  • Also consider changing `edmx:StorageModels/Schema/EntityContainer@Name` and `edmx:Mappings/Mapping/EntityContainerMapping@StorageEntityContainer` attributes on actual database schema - this helped in my case. – stop-cran Oct 10 '17 at 10:03
  • Thank you, I would not have gotten this in the time i needed it done. Not all tables need to have primary keys. – gsirianni Jan 28 '18 at 07:11
12

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"
wctiger
  • 921
  • 12
  • 22
  • Thanks for doing this! I wish someone could explain how to create the insert/update function though also... – JDPeckham Sep 29 '17 at 22:19
  • Can I give you a +50 for this, been ignoring this issue for years, sometimes my tables after an "Update From Database" get stuffed up so I revert the change, never had to figure it out until today, wish I knew this years ago... – Chris Schaller May 22 '18 at 05:47
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 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

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
4

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

Rajdeep
  • 788
  • 7
  • 26
2

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

2

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

Moji
  • 5,720
  • 2
  • 38
  • 39
1

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

bajran
  • 1,433
  • 14
  • 23
0

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)