-1

I am trying to save the data in the database. Table name is product_color.

public void PostAddColour1()
{
     product_color pc = new product_color();
     pc.id = 999;
     pc.product_id=2;
     pc.color_id=1;
     pc.display_order=3;
     db.product_color.Add(pc);
     db.SaveChanges();
}

This is the function code to insert the data into table.

when db.SaveChanges(); is called the below error shows:

Unable to update the EntitySet 'product_color' because it has a
DefiningQuery and no <InsertFunction> element exists in the 
ModificationFunctionMapping> element to support the current operation.
Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
Sajjadd Hussain
  • 15
  • 2
  • 2
  • 10
  • Does your Table 'product_color' have Primary Key ? – Pirate X May 31 '16 at 05:55
  • Possible duplicate of [Unable to update the EntitySet - because it has a DefiningQuery and no element exist](http://stackoverflow.com/questions/7583770/unable-to-update-the-entityset-because-it-has-a-definingquery-and-no-updatefu) – Federico Dipuma May 31 '16 at 12:48

1 Answers1

1

This usually happens when you have not defined any primary key on your table. When a table doesn't have a PK then the Entity Framework considers it to be a View and not a table and hence it doesn't allow data to be directly inserted to it.

Simply set a Primary Key on the table. That should do the trick

You can read more on this here

Pirate X
  • 3,023
  • 5
  • 33
  • 60