简体   繁体   English

泛型检查实体框架和存储库模式是否有所更改

[英]Generic check if something has changed with entity framework and repository pattern

My update method will always update since I have to set the LastModified date. 由于必须设置LastModified日期,所以我的更新方法将始终更新。 I was wondering if there is a way to dynamically check if some values have changed. 我想知道是否有一种方法可以动态检查某些值是否已更改。

My state objects look like this: 我的状态对象如下所示:

public partial class Action : IEntity
{
    public long Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public System.DateTime Created { get; set; }
    public System.DateTime LastModified { get; set; }
    public Nullable<System.DateTime> Deleted { get; set; }
}

The interface I use looks like this: 我使用的界面如下所示:

public interface IEntity
{
    long Id { get; set; }        
    DateTime Created { get; set; }
    DateTime LastModified { get; set; }
    DateTime? Deleted { get; set; }       
}

My update method looks like this (The changes are saved later): 我的更新方法如下所示(更改将在以后保存):

    public virtual void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        var attachedEntity = DbSet.Find(entity.Id);

        if (attachedEntity != null)
        {
            var attachedEntry = DbContext.Entry(attachedEntity);

            entity.Created = attachedEntity.Created;
            entity.LastModified = DateTime.Now;

            attachedEntry.CurrentValues.SetValues(entity);
        }
        else
        {
            dbEntityEntry.State = EntityState.Modified;
            entity.LastModified = DateTime.Now;
        }
    }

So it will actually perform a generic update for every object that is passed as T with the the IEntity Interface. 因此,它实际上会对通过IEntity接口作为T传递的每个对象执行通用更新。 However it performs the update every single call to this method because the LastModified value is changed. 但是,由于LastModified值已更改,因此每次调用此方法都会执行更新。 Resulting in many update queries like these: 导致许多更新查询如下:

exec sp_executesql N'update [dbo].[BiztalkEntity]
set [LastModified] = @0
where ([Id] = @1)
',N'@0 datetime2(7),@1 bigint',@0='2013-05-17 11:22:52.4183349',@1=10007

Could you tell me how to prevent the query being executed every single time? 您能告诉我如何防止每次执行查询吗?

I suggest you delay the setting of LastModified and let Entity Framework give you all the entities that have been changed just prior to the changes being sent to the database. 我建议您延迟LastModified的设置,并让Entity Framework为您提供在将更改发送到数据库之前已更改的所有实体。

You can override the SaveChanges() method of DbContext 您可以重写DbContextSaveChanges()方法

public class MyContext : DbContext
{
    public override int SaveChanges()
    {
        //you may need this line depending on your exact configuration
        //ChangeTracker.DetectChanges();
        foreach (DbEntityEntry o in GetChangedEntries())
        {
            IEntity entity = o.Entity as IEntity;
            entity.LastModified = DateTime.Now;
        }
        return base.SaveChanges();
    }

    private IEnumerable<DbEntityEntry> GetChangedEntries()
    {
        return new List<DbEntityEntry>(
            from e in ChangeTracker.Entries()
            where e.State != System.Data.EntityState.Unchanged
            select e);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM