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

允我心安 提交于 2019-12-05 13:43:00

问题


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?


回答1:


We haven't implemented data annotations yet. (See #107) You should be able to do it using the Fluent API.

modelBuilder.Entity<MyEntity>().Ignore(e => e.NotMappedProperty);



回答2:


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<Blog> Blogs { get; set; }
    
         protected override void OnModelCreating(ModelBuilder modelBuilder)
         {
             modelBuilder.Entity<Blog>()
             .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.



来源:https://stackoverflow.com/questions/27540959/how-can-i-stop-ef-7-mapping-an-entity-property-to-a-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!