I use code first(entity framework). when i call DbContext.SaveChanges(), I get a primary key constraint because here is already such a record. I want to override the record if it already exists in the database. Which is the simplest way to do this. Checking each time I call savechanges for repeting primary keys is too hard in my project. Thanks
preguntado el 08 de enero de 11 a las 18:01
Unfortunately EF demands that you know if you are adding or modifing entity. So only ways are:
- Keep this knowledge in your application and set proper EntityState in ObjectStateManager.
- Load entity first. If exists modify its data, if doesn't create new entity and add it to context.