Scenario:
I have following set of entities:
public interface IAuditable
{
DateTime? CreatedDate { get; set; }
string CreatedBy {get;set;}
string LastModifiedBy {get;set;}
DateTime? LastModifiedDate {get;set;}
}
public abstract class BaseEntity: IAuditable
{
public int ID { get; set; }
public DateTime? CreatedDate { get; set; }
public string CreatedBy {get;set;}
public string LastModifiedBy {get;set;}
public DateTime? LastModifiedDate {get;set;}
}
public class Product: BaseEntity
{
public string Name { get; set; }
}
public class Order : BaseEntity
{
public int ProductID { get; set; }
public int Qty { get; set; }
}
and following DbContext:
public class AuditDbContext : DbContext
{
public AuditDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Products>()
.Ignore(t => t.CreatedDate);
modelBuilder.Entity<Order>()
.Ignore(t => t.LastModifiedBy);
modelBuilder.Entity<Order>()
.Ignore(t => t.LastModifiedDate);
}
public override int SaveChanges()
{
SetAuditableProperties();
return base.SaveChanges();
}
private void SetAuditableProperties()
{
foreach (var auditableEntity in ChangeTracker.Entries<IAuditable>())
{
if (auditableEntity.State == EntityState.Added ||
auditableEntity.State == EntityState.Modified)
{
string userName = "ABC";
DateTime dateTimeNow = DateTime.Now;
auditableEntity.Entity.LastModifiedDate = dateTimeNow;
auditableEntity.Entity.LastModifiedBy = userName;
if (auditableEntity.State == EntityState.Added)
{
auditableEntity.Entity.CreatedDate = dateTimeNow;
auditableEntity.Entity.CreatedBy = userName;
}
else
{
auditableEntity.Property(p => p.CreatedDate).IsModified = false; //TODO: Set this property if mapped otherwise dont execute this line
auditableEntity.Property(p => p.CreatedBy).IsModified = false; //TODO: Set this property if mapped otherwise dont execute this line
}
}
}
}
}
Observe I'm Ignoring some properties form entities in OnModelCreating and setting Auditable properties inside SaveChanges method by overriding it. Now, when I try to create new entity it works as expected but when I try to update an existing entity it throws exception.
Problem:
Let's say I try to update Product entity it throws following exceptions:
"Member 'IsModified' cannot be called for property 'CreatedDate' on entity of type 'Products' because the property is not part of the Entity Data Model".
Same is true for 'Order' entity but for 'LastModifiedBy' and 'LastModifiedDate' properties.
How can I check if a property is ignored from entity inside 'SetAuditableProperties' method? So that I can set 'IsModified' accordingly.
P.S. I can find out [NotMapped] properties but when I use the ModelBuilder to ignore a column, I cannot detect these.
CreationDate
is not part of the model, that isCreationDate
will never be nor read nor written to the database ?