简体   繁体   中英

Entity Framework check current value before update

We have a repository for CRUD operations. Basically if we want to do any of the read/write operations we can do something like

    using (MemberRepo mRepo = new MemberRepo())
    {
         mRepo.UpdateEntity(member);
         mRepo.Save();
    }

What I need to know however is if it is possible to know a certain column value in the database before we update it? For example if there is a field in the member table called Status and I want to know the current existing status of that member before I update it with some other value?

Thanks.

The following code will let you set the creation date of any entity that has this column/attribute. It can work as a trigger in some cases.

 public override int SaveChanges()
    {
        foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("CreationDate") != null))
        {
            if (entry.State == EntityState.Added)
            {
                entry.Property("CreationDate").CurrentValue = DateTime.Now;
            }
        }
        return base.SaveChanges();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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