Fixing the System.Data.UpdateException – DefiningQuery and no <InsertFunction> Error

Here’s another in my never-ending series of fixes for dumb mistakes. In this episode we lose time with an Entity Framework error. Here’s the error message:

System.Data.UpdateException was unhandled by user code
  Message="Unable to update the EntitySet 'Purchases' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation."
  Source="System.Data.Entity"

This one had me baffled because I was mainly just cloning ASP.NET code that worked fine against a very similar database table. The code looked like this, with the error on the last line in boldface:

  Dim ent As New DBEntities1
  Dim pchase As New DBModel.Purchases
  pchase.OrderNumber = CleanString(txtPurchaseNumber.Text)

' ……….
  pchase.DateCreated = Now
  pchase.LastMod = Now
  ent.AddToPurchases(pchase)
  ent.SaveChanges()

I finally tracked it down to the SQL Server database table that I had recently added. I hadn’t marked the ID (identity) field as the primary key. (I thought I had but it wasn’t there when I went to look!) After that, just regenerate the .edmx file and the error is gone.

When I confide to my wife that I’ve made a stupid programming mistake, she insists that “surely others must do the same”. That might be the case, but most developers aren’t crazy enough to write a blog post to expose their folly in perpetuity to potential clients. <grin>

That said, I take comfort in the hope that you’ve discovered this little fix before losing time like I did.

Ken

Published 09 September 2009 06:56 PM by Ken Cox [MVP]

Comments

# yash said on 21 September, 2009 04:18 AM

Well i hope you are not the only one. Thanks for the Post.

# Jos Verbeek said on 27 September, 2009 02:00 PM

Thanks for writing about your mistake, I had made the same one and thanks to you it didn't take me to long!!

# Ken Cox [MVP] said on 27 September, 2009 02:07 PM

@Jos: It's good to hear that my goal of reducing aggravation for others is being met.

It helps offset the feeling that I'm looking like the village idiot around here. <grin>

Ken

# Greg Goodall said on 29 September, 2009 11:05 PM

Ken, I had the same error and your comment about the Identity column not also being set as primary key solved the problem.  thanks a ton.

# Ken Cox [MVP] said on 30 September, 2009 12:30 AM

@Greg: Thanks for checking in. This makes me wonder why the error message doesn't offer the primary key issue as a possible fix.

Ken

# Kevin Haesendonckx said on 15 October, 2009 07:30 AM

Thanks man, this helped me a lot, keep up the good work

# purs said on 22 October, 2009 01:25 AM

Thank you :) the post really helped me to figure out the error quickly.

# squorpeeon said on 31 October, 2009 11:01 AM

At least you didn't called a "dumb**s".

Thanks for the quick fix.

# Paul said on 24 November, 2009 10:27 PM

I made the same mistake , and now I fixed it ,thanks for your kindness.

# Paul said on 24 November, 2009 10:30 PM

I made the same mistake , and now I fixed it ,thanks for your kindness.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

Web Links

Syndication