Updaing \ save with EF violates Uniquness DB restriction

I use EF to update an object.

Here is my code:

public MamConfiguration_V1 Save(MamConfiguration_V1 item) { item.ThrowIfNull("item"); mMaMDBEntities.MamConfiguration_V1.AddObject(item); mMaMDBEntities.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified); mMaMDBEntities.SaveChanges(); return item; } 

However, this way I get por erorr violation in the database.

I prefer to use this method (creating a new EF object) and marking it as

changed, how can I not break the uniquness restriction?

0
source share
1 answer

If you use this method to update an existing item, you get an exception due to

  mMaMDBEntities.MamConfiguration_V1.AddObject(item); 

lines. It seems like using this code you are trying to put an element in your table for the second time. If I guessed correctly, and you use this method to update existing elements, you just need to delete this line - EF will automatically update the database in accordance with the changes you made to the fields of your object.

-2
source

Source: https://habr.com/ru/post/898560/


All Articles