Ankit Yadav

Ankit Yadav

  • NA
  • 762
  • 36.8k

Ignore properties in data model while keeping them in EF CoreMigration

Jul 3 2020 8:32 AM
I have properties in my Model like
  1. public class Test{    
  2.         public int Id{ getset; }    
  3.         public string Title { getset; }    
  4.         public DateTime? CreatedDate { getset; }    
  5.         public DateTime? ModifiedDate { getset; }    
  6.         public DateTime? DeletedDate { getset; }    
  7. }  
Here, I am using this Test class for creating Table in Database using Migration, Now the table is created successfully but the problem is when i want do any operation using stored procedure which is like " Select Title from Test where Id=1" ,When i run the this i am facing error like this
 
The required column 'CreatedDate' was not present in the results of a 'FromSql' operation"
 
I tried
  1. NotMapped Attribute it works fine but when i add another migration the NotMapped properties gets Dropped from the database after updating the database
  2. Also use Shadow properties and Ignore properties like
    1. protected override void OnModelCreating(ModelBuilder modelBuilder)  
    2. {  
    3. modelBuilder.Entity<Test>().Property<DateTime?>("CreatedDate");  
    4. modelBuilder.Entity<Test>().Property<DateTime?>("ModifiedDate");  
    5. modelBuilder.Entity<Test>().Property<DateTime?>("DeletedDate");  
    6. }  
  3. Also try this
    1. protected override void OnModelCreating(ModelBuilder modelBuilder) {  
    2. modelBuilder.Entity<Test>().Ignore(x => x.DeletedDate);  
    3. modelBuilder.Entity<Test>().Ignore(x => x.IsDeleted);  
    4. modelBuilder.Entity<Test>().Ignore(x => x.ModifiedDate); }  
But the issue remains the same ,
 
So the issue is i want to ignore the CreateDate, ModifiedDated, DeletedDated property while performing DB operation and also not want to drop these columns from Database when i add and update new migration. Can anyone guide me what i need to do to resolve this issue.
 
Reference link: https://stackoverflow.com/questions/47057856/ignore-properties-in-data-model-while-keeping-them-in-ef-core-migrations

Answers (4)