14

I have an entity which is not connected to my dbcontext. I want to change that. However there is already another instance of the same entity attached to dbcontext. If I just add my new entity, I get an error, that an entity with the same primary key is already attached.
I tried multiple different variants of removing the old entity from dbcontext without any success. How can I replace the old instance with the new one?
Note: I don't want to copy the values, I want to attach this very instance of my new entity to dbcontext.

var entity = new MyEntity { Id = 1 };
var logicalDuplicate = dbcontext.Set<MyEntity >().Local
    .FirstOrDefault(e => e.Equals(entity));
if (logicalDuplicate != null)
{
    // remove logicalDuplicate from dbcontext
}
dbcontext.MyEntity.Attach(entity);

For clarification: I have overridden Equals to check for Id instead of reference.

10
  • Have you tried using the refresh method? dbcontext.Refresh(RefreshMode.ClientWins, entity) Jun 2, 2016 at 9:54
  • @FlorianHaider dbcontext doesn't have Refresh. But even if, I don't think this would solve the issue. Jun 2, 2016 at 10:02
  • My mistake, you have to cast it to ((IObjectContextAdapter)dbcontext).ObjectContext. Jun 2, 2016 at 10:13
  • @FlorianHaider I clarified my question: I don't want to update the properties. I want to attach this very instance of my entity to dbcontext. Refresh is not actually helping me with this. Jun 2, 2016 at 11:22
  • 2
    As far as I know there is no easy way. You can either dig into Reflections to get a list of navigation properties and detach them, or look into ObjectContext -> ObjectStateManager -> GetRelationshipManager() and go through the related ends, not sure about that as I have not tried it myself. Jun 6, 2016 at 11:55

3 Answers 3

12

Try this:

if (logicalDuplicate != null)
{
    dbcontext.Entry(logicalDuplicate).State = EntityState.Detached;
    dbcontext.MyEntity.Attach(entity);
    dbcontext.Entry(entity).State = EntityState.Modified;
}
else
{
    dbcontext.MyEntity.Add(entity);
}

How to get related entries

I investigated that and want to share with my results. I used reflection as short way to get entity properties names. But it's possible to get it without reflection as mentioned @Florian Haider. You can use answer and this.

// Found loaded related entries that can be detached later.
private HashSet<DbEntityEntry> relatedEntries;

private DbContext context;

private List<string> GetPropertiesNames(object classObject)
{
    // TODO Use cache for that.
    // From question https://stackoverflow.com/questions/5851274/how-to-get-all-names-of-properties-in-an-entity
    var properties = classObject.GetType().GetProperties(BindingFlags.DeclaredOnly |
                                                              BindingFlags.Public |
                                                              BindingFlags.Instance);
    return properties.Select(t => t.Name).ToList();
}

private void GetRelatedEntriesStart(DbEntityEntry startEntry)
{
    relatedEntries = new HashSet<DbEntityEntry>();

    // To not process start entry twice.
    relatedEntries.Add(startEntry);
    GetRelatedEntries(startEntry);
}

private void GetRelatedEntries(DbEntityEntry entry)
{
    IEnumerable<string> propertyNames = GetPropertiesNames(entry.Entity);
    foreach (string propertyName in propertyNames)
    {
        DbMemberEntry dbMemberEntry = entry.Member(propertyName);
        DbReferenceEntry dbReferenceEntry = dbMemberEntry as DbReferenceEntry;
        if (dbReferenceEntry != null)
        {
            if (!dbReferenceEntry.IsLoaded)
            {
                continue;
            }

            DbEntityEntry refEntry = context.Entry(dbReferenceEntry.CurrentValue);
            CheckReferenceEntry(refEntry);
        }
        else
        {
            DbCollectionEntry dbCollectionEntry = dbMemberEntry as DbCollectionEntry;
            if (dbCollectionEntry != null && dbCollectionEntry.IsLoaded)
            {
                foreach (object entity in (ICollection)dbCollectionEntry.CurrentValue)
                {
                    DbEntityEntry refEntry = context.Entry(entity);
                    CheckReferenceEntry(refEntry);
                }
            }
        }
    }
}

private void CheckReferenceEntry(DbEntityEntry refEntry)
{
    // Add refEntry.State check here for your need.
    if (!relatedEntries.Contains(refEntry))
    {
        relatedEntries.Add(refEntry);
        GetRelatedEntries(refEntry);
    }
}
4
  • Strange, worked for me - all properties were update on save. Other way is to copy all properties to loaded entity. You may use AutoMapper for that. Jun 2, 2016 at 10:10
  • I'm not talking about updating properties but attaching this very instance to dbcontext. Should have been more clear, will edit the question later. Jun 2, 2016 at 11:14
  • Ok I have to correct myself. Your approach seems to work. My problem is, that my entity is connected to other entities which are still attached to the context. Do you know of a simple way to recursively detach all connected entities as well? Jun 6, 2016 at 10:10
  • 1
    @tim-pohlmann I added to answer code sample of how to get related entries. Jun 8, 2016 at 4:32
1

Edit This finds the original product, removes it, and adds the new one:

    static void UpdateDatabase() 
    {
        Context context = new Context();
        Product product1 = context.Products.Find(1);
        context.Products.Remove(product1);
        Product product2 = new Product(){ProductId = 1, Name = "Product2"};
        context.Products.Add(product2);
        context.SaveChanges();
    }
3
  • I tried context.MyEntity.Remove(logicalDuplicate); context.MyEntity.Attach(entity); and it doesn't work. Jun 2, 2016 at 13:33
  • Did you try context.MyEntity.Add(entity); ?
    – poppertech
    Jun 2, 2016 at 14:11
  • I tried add and it seems to work at first but for some reason in some special case lazy loading doesn't work...I guess that might be connected to another issue, thanks for your help anyway. Jun 6, 2016 at 10:16
-2

Best way to salve this problem is


db is my database Object
updateprice is my database entity object
ep is my old same database entity object

db.Entry(updateprice).CurrentValues.SetValues(ep);

1
  • 1
    I quote from the question: "I don't want to copy the values, I want to attach this very instance of my new entity to dbcontext." May 4, 2017 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.