EntityFramework Core 学习笔记 —— 包含与排除属性

原文地址:[https://docs.efproject.net/en/latest/modeling/included-properties.html][1]

在模型中包含一个属性意味着 EF 拥有了这个属性的元数据并且将尝试在数据库中进行读写属性的值。

内容导航

  • [约定][2]
  • [Data Annotation][3]
  • [Fluent API][4]

约定

根据约定,带有 getter 和 setter 的公共属性将被包含在模型中。

Data Annotations

我们可以使用 Data Annotaions 来从模型中排除一个属性。
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    [NotMapped]    // 人工高亮
    public DateTime LoadedFromDatabase { get; set; }
}

Fluent API

我们可以使用 Fluent API 来从模型中排除一个属性。 ``` class MyContext : DbContext { public DbSet 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; }

}



  [1]: https://docs.efproject.net/en/latest/modeling/included-properties.html
  [2]:#1
  [3]:#2
  [4]:#3
posted @ 2016-08-03 22:38  不如隐茶去  阅读(961)  评论(0编辑  收藏  举报