简体   繁体   中英

Configure automatic update of base class properties whenever derived class is updated EF Core 2.0

I have a base class called EntityBase which is used on every entity modeled in this project. This base class uses the Repository Pattern to perform operations on theses entities as shown below.

public class Repository : IRepository
{
    private readonly EfCoreContext _dbContext;

    public Repository(EfCoreContext dbContext)
    {
        _dbContext = dbContext;
    }


    public IQueryable<T> All<T>() where T : EntityBase
        => _dbContext.Set<T>().AsQueryable();

    public IEnumerable<T> Filter<T>(Expression<Func<T, bool>> predicate) where T : EntityBase
        => Filter<T>(predicate).AsQueryable<T>();

    ...
    ...

    public T Add<T>(T entity) where T : EntityBase
    {
        _dbContext.Set<T>().Add(entity);
        _dbContext.SaveChanges();

        return entity;
    }
}

And here's the Base Class

public abstract class EntityBase
{
    public EntityBase()
    {
        this.CreatedOn = DateTime.Now;
        this.UpdatedOn = DateTime.Now;
        this.CreatedBy = "DC";
        this.UpdatedBy = "DC";
    }

    public string CreatedBy { get; set; }
    public string UpdatedBy { get; set; }

    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }

    public int ID { get; set; }
}

How do I use the Fluent API or Data Annotations to automatically capture the UpdatedOn and UpdatedBy properties of the EntityBase whenever any one of it's derived classes are updated?

Here is my DbContext

public class EfCoreContext : DbContext
{
    public EfCoreContext(                             
        DbContextOptions<EfCoreContext> options)      
        : base(options) {}

    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<Order> Orders { get; set; }

    protected override void
        OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new BookConfig());       
        modelBuilder.ApplyConfiguration(new BookAuthorConfig());
        modelBuilder.ApplyConfiguration(new OrderConfig());
        modelBuilder.ApplyConfiguration(new LineItemConfig());   
    }
}

First: You probably shouldn't have two repositories. EF is already a generic repository with a UoW called DbContext. Read more about it here .

If you can live to wait until SaveChanges is called, you could overrite your DbContext's SaveChanges method:

public override int SaveChanges()
{
    var entries = ChangeTracker.Entries().Where(x => x.Entity is EntityBase && (x.State == EntityState.Added || x.State == EntityState.Modified));
    foreach (var entry in entries)
    {
        var entity = (EntityBase)entry.Entity;
        //do something
    }
    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