Entity Framework 6.1.1 - How to check if property is ignored from an entity in SaveChanges method

c# dbcontext entity-framework entity-framework-6

Question

Scenario:

I have the entities listed below:

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 the subsequent 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
                    }
                }
            }
        }
    }

See how I override the SaveChanges function to add Auditable attributes while ignoring certain entity-specific properties in OnModelCreating. Now, when I attempt to create a new entity, everything goes as planned, but when I attempt to change an existing object, an exception is thrown.

Problem:

If I attempt to edit the Product object, the following exceptions are thrown:

Because the property "CreatedDate" on entities of type "Products" is not included in the Entity Data Model, member "IsModified" cannot be called for that property..

The same is true for the LastModifiedBy and LastModifiedDate fields of the "Order" object.

How can I determine if a property from an object is ignored within the "SetAuditableProperties" method? so that I may appropriately adjust "IsModified."

P.S. When I use the ModelBuilder to disregard a column, I am able to identify [NotMapped] properties, but I am unable to do so with these.

1
0
6/7/2017 5:30:54 PM

Accepted Answer

ZZZ_tmp
4
6/7/2017 8:23:58 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow