UPDATE. I got it working using this code
EntityState entityState = _siriusContext.GetEntityState(entity);
if (entityState == EntityState.Detached)
{
_siriusContext.Set<Closeout>().AddOrUpdate(entity);
}
but haven't yet verified the EF generated statements (the end result is OK so far - all is saved correctly).
Hi everybody,
I am wondering if someone knows how to approach this problem.
I have a class that has many simple properties (that correspond to table's columns) but also has a few related virtual ICollection properties for different classes (related tables).
I have the following code in the Update method:
EntityState entityState = _siriusContext.GetEntityState(entity);
if (entityState == EntityState.Detached)
{
_dbSet.Attach(entity);
_siriusContext.SetModified(entity);
}
I only want EF to update the "main" table properties and I created a stored procedure to manually update related table by passing table-valued parameter to it.
The problem is that Attach and SetModified is not working because my related entity may have new rows (PK is not yet set).
The error I am getting is the following:
Attaching an entity of type 'Siriusware.Models.Ovrshort' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
I understand the reason behind this message but I don't know what should I do to fix it. I only want to let EF update the main table and update related Overshot table myself manually through the procedure.
Do you know what can I do?
Thanks in advance.
If it's not broken, fix it until it is.
My Blog