MVC 2 : Fixing the System.Data.UpdateException – DefiningQuery and no 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.”

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

Solution : [I’m pretty sure that entity requires a primary key. It’s a pain when a table has no primary key or if the table has a composite key..]

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s