How can I stop EF 7 mapping an entity property to a column?

后端 未结 2 2020
傲寒
傲寒 2021-02-19 11:07

In EF 6, I can add a NotMapped attribute to the property, then it will not be mapped to a column. How can I do this in EF 7?

2条回答
  •  暖寄归人
    2021-02-19 11:43

    Just to add on to Ricky and bricelam's answer,

    There are two ways to ignore a property:

    1. Data annotations on model

      public class Blog
      {
          public int BlogId { get; set; }
          public string Url { get; set; }
      
          [NotMapped]
          public DateTime LoadedFromDatabase { get; set; }
      }
      
    2. Fluent API overriding OnModelCreating

      class MyContext : DbContext
       {
           public DbSet Blogs { get; set; }
      
           protected override void OnModelCreating(ModelBuilder modelBuilder)
           {
               modelBuilder.Entity()
               .Ignore(b => b.LoadedFromDatabase);
           }
       }
      
       public class Blog
       {
           public int BlogId { get; set; }
           public string Url { get; set; }
      
           public DateTime LoadedFromDatabase { get; set; }
       }
      

    Documentation available here.

提交回复
热议问题