如何停止EF 7将实体属性映射到列?

时间:2014-12-18 07:06:52

标签: entity-framework-core

在EF 6中,我可以向属性添加NotMapped属性,然后它不会映射到列。我怎么能在EF 7中做到这一点?

2 个答案:

答案 0 :(得分:15)

我们尚未实施数据注释。 (请参阅#107)您应该可以使用Fluent API执行此操作。

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

答案 1 :(得分:13)

只是为了增加Ricky和bricelam的答案,

有两种方法可以忽略属性:

  1. 模型上的数据注释

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    
        [NotMapped]
        public DateTime LoadedFromDatabase { get; set; }
    }
    
  2. Fluent API覆盖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; }
     }
    
  3. 可用文档here

相关问题