用同一实体的不同实例替换上下文中的实体

Replace entity in context with a different instance of the same entity

我有一个与我的 dbcontext 没有关联的实体。我想改变它。但是,dbcontext 已经附加了同一实体的另一个实例。如果我只是添加我的新实体,我会收到一个错误消息,即已经附加了具有相同主键的实体。
我尝试了多种从 dbcontext 中删除旧实体的不同变体,但均未成功。如何用新实例替换旧实例?
注意:我不想复制这些值,我想将我的新实体的这个实例附加到 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);

澄清一下:我已覆盖 Equals 以检查 Id 而不是参考。

试试这个:

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

如何获取相关条目

我对此进行了调查,并希望与我分享我的结果。 我使用反射作为获取实体属性名称的捷径。但是正如@Florian Haider 提到的那样,可以不经思考就得到它。您可以使用 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 
    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);
    }
}

编辑 这会找到原始产品,将其删除,然后添加新产品:

    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();
    }

解决此问题的最佳方法是


db 是我的数据库对象
updateprice 是我的数据库实体对象
ep 是我以前的同一个数据库实体对象

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