EF7 - 如何在更改前從DbSet獲取值

asp.net c# entity-framework entity-framework-core linq

我嘗試在代碼更改之前和保存之前獲取存儲在DbSet中的實體的值。但是,當我嘗試使用LINQ Single語句時,我得到了更改的值。我正在使用EF7。

這是代碼:

DbSet<Entity> dbSet = Context.dbSet;
Entity ent = dbSet.Single(x => x.Id == id);
ent.FirstName = "New name";
Entity entityBeforeChange = dbSet.Single(x => x.Id == id);  //here I want to get entity with old values, if that's important I just need to read it without modifying this instance

Context.SaveChanges();

希望我足夠清楚,可以得到一些幫助

一般承認的答案

您可以從上下文中Detach實體。請記住, 更新其他附加實體之前 ,您必須從上下文中提取它。

DbSet<Entity> dbSet = Context.dbSet;
Entity ent = dbSet.Single(x => x.Id == id);
Entity entityBeforeChange = dbSet.Single(x => x.Id == id); 
Context.Entry(entityBeforeChange).State = EntityState.Detached; // severs the connection to the Context
ent.FirstName = "New name";

Context.SaveChanges();

專家解答

這是我在審計庫中使用的代碼。

EF7

using (var ctx = new TestContext())
{
    Entity ent = entity.Single(x => x.Id == id);
    entity.FirstName = "New name";

    context.ChangeTracker.DetectChanges();

    // Find your entry or get all changed entries
    var changes = context.ChangeTracker.Entries().Where(x => x.State == EntityState.Modified);

    foreach (var objectStateEntry in changes)
    {
        AuditEntityModified(audit, objectStateEntry, auditState);
    }
}

public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntry objectStateEntry)
{
    foreach (var propertyEntry in objectStateEntry.Metadata.GetProperties())
    {
        var property = objectStateEntry.Property(propertyEntry.Name);

        if (entry.Parent.CurrentOrDefaultConfiguration.IsAudited(entry.ObjectStateEntry, propertyEntry.Name))
        {
            entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue));
        }
    }
}

EF6

using (var ctx = new TestContext())
{
    Entity ent = entity.Single(x => x.Id == id);
    entity.FirstName = "New name";

    context.ChangeTracker.DetectChanges();

    // Find your entry or get all changed entries
    var changes = context.ChangeTracker.Entries().Where(x => x.State == EntityState.Modified);

    foreach (var objectStateEntry in changes)
    {
        AuditEntityModified(audit, objectStateEntry, auditState);
    }
}

public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntry objectStateEntry)
{
    foreach (var propertyEntry in objectStateEntry.Metadata.GetProperties())
    {
        var property = objectStateEntry.Property(propertyEntry.Name);

        if (entry.Parent.CurrentOrDefaultConfiguration.IsAudited(entry.ObjectStateEntry, propertyEntry.Name))
        {
            entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue));
        }
    }
}

編輯:完整的源代碼可以在我的GitHub存儲庫中找到: https//github.com/zzzprojects/EntityFramework-Plus

圖書館網站: http//entityframework-plus.net/




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因