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


在模型类中包含一种类型意味着 EF 拥有了这种类型的元数据并且将尝试在数据库中进行读写类型的实例。

内容导航

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

约定

根据约定,暴露在你的上下文的 `DbSet` 属性中的类型将会被包含入你的模型中。此外,在 `OnModelCreating` 方法中涉及到的类型也会被包含进来。最终,任何在已被发现的类型被递归查找到的属性的类型也会被包含在模型中。
> By convention, types that are exposed in `DbSet` properties on your context are included in your model. In addition, types that are mentioned in the `OnModelCreating` method are also included. Finally, any types that are found by recursively exploring the navigation properties of discovered types are also included in the model.
*最后一句话好像翻译的有问题*

举个栗子!在下面的代码中,所有的三个类型都将会被发现

  • Blog,因为它暴露在上下文的 DbSet 属性中
  • Post,因为它被 Blog.Posts 这个导航属性找到
  • AuditEntry,因为它在 OnModelCreating 涉及到了
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; } // 人工高亮 protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditEntry>(); // 人工高亮
}
} public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public List<Post> Posts { get; set; } // 人工高亮
} public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; } public Blog Blog { get; set; }
} public class AuditEntry
{
public int AuditEntryId { get; set; }
public string Username { get; set; }
public string Action { get; set; }
}

Data Annotation

我们也可以通过 Data Annotations 来从模型中排除一个类型

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; } public BlogMetadata Metadata { get; set; }
} [NotMapped] // 人工高亮
public class BlogMetadata
{
public DateTime LoadedFromDatabase { get; set; }
}

Fluent API

我们也可以使用 Fluent API 从模型中排除一个类型。
```
class MyContext : DbContext
{
public DbSet Blogs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<BlogMetadata>(); // 人工高亮
}

}

public class Blog

{

public int BlogId { get; set; }

public string Url { get; set; }

public BlogMetadata Metadata { get; set; }

}

public class BlogMetadata

{

public DateTime LoadedFromDatabase { get; set; }

}



  [1]: https://docs.efproject.net/en/latest/modeling/included-types.html
[2]: #1
[3]: #2
[4]: #3

EntityFramework Core 学习笔记 —— 包含与排除类型的更多相关文章

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

    原文地址:https://docs.efproject.net/en/latest/modeling/included-properties.html 在模型中包含一个属性意味着 EF 拥有了这个属性 ...

  2. EntityFramework Core 学习笔记 —— 创建模型

    原文地址:https://docs.efproject.net/en/latest/modeling/index.html 前言: EntityFramework 使用一系列的约定来从我们的实体类细节 ...

  3. EntityFramework Core 学习笔记 —— 添加主键约束

    原文地址:https://docs.efproject.net/en/latest/modeling/keys.html Keys (primary) Key 是每个实体例的主要唯一标识.EF Cor ...

  4. EntityFramework Core 学习系列(一)Creating Model

    EntityFramework Core 学习系列(一)Creating Model Getting Started 使用Command Line 来添加 Package  dotnet add pa ...

  5. .NET CORE学习笔记系列(2)——依赖注入[7]: .NET Core DI框架[服务注册]

    原文https://www.cnblogs.com/artech/p/net-core-di-07.html 包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IS ...

  6. .NET CORE学习笔记系列(2)——依赖注入[6]: .NET Core DI框架[编程体验]

    原文https://www.cnblogs.com/artech/p/net-core-di-06.html 毫不夸张地说,整个ASP.NET Core框架是建立在一个依赖注入框架之上的,它在应用启动 ...

  7. .NET CORE学习笔记系列(2)——依赖注入[4]: 创建一个简易版的DI框架[上篇]

    原文https://www.cnblogs.com/artech/p/net-core-di-04.html 本系列文章旨在剖析.NET Core的依赖注入框架的实现原理,到目前为止我们通过三篇文章从 ...

  8. .NET CORE学习笔记系列(2)——依赖注入【3】依赖注入模式

    原文:https://www.cnblogs.com/artech/p/net-core-di-03.html IoC主要体现了这样一种设计思想:通过将一组通用流程的控制权从应用转移到框架中以实现对流 ...

  9. .NET CORE学习笔记系列(2)——依赖注入【1】控制反转IOC

    原文:https://www.cnblogs.com/artech/p/net-core-di-01.html 一.流程控制的反转 IoC的全名Inverse of Control,翻译成中文就是“控 ...

随机推荐

  1. js 进度条,可实现结束和重新开始

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. HTML5文件API

    File对象与File对象 Blob对象 FileReader对象 File对象 <!DOCTYPE html> <html> <head> <meta ht ...

  3. 在strut.xml 中使用ognl

    在struts.xml 中使用ognl有两种方面的需求: 1. 在action执行时从struts.xml中读取param标签中的值,然后调用标签name属性相应的set方法对action中的变量赋值 ...

  4. IOS第12天(2,UINavigationController导航控制器)

    ****HMAppDelegate.m @implementation HMAppDelegate - (BOOL)application:(UIApplication *)application d ...

  5. 【iCore3 双核心板_FPGA】实验二十三:使用JTAG UART终端打印信息

    实验指导书及代码包下载: http://pan.baidu.com/s/1c83OPC iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...

  6. 【7集iCore3基础视频】7-5 iTool2驱动安装

    iTool2驱动安装: 高清源视频:链接:http://pan.baidu.com/s/1dF5FtlB%20密码:g5x7 iCore3 购买链接:https://item.taobao.com/i ...

  7. Android应用字体更改

    首先下载字库 中华字体网 然后在项目的assets目录下建立文件夹fonts.将字体库文件xxx.ttf放入 然后使用下面工具类,自定义控件自己注意添加 public class TypefaceTo ...

  8. Linux系统下Redis安装(二)

    上一篇随笔,已经成功开启了Redis-server,客户端已经可以正常对redis进行正常操作. 但是,redis-server开启的同时,一个大大doc黑屏也在那里开启着,接下来就对讲解如何在Lin ...

  9. 使用CMD命令设置IP

    使用CMD命令设置IP,将下面文本保存为bat文件后执行 netsh interface ip set address name="本地连接" source=static addr ...

  10. [QGLViewer]First Demo

    Dependencies: QGLViewer2.6.3 Win7 64bit VS2010 直接使用QGLViewer文件夹下的Pro文件在VS2010中可以编译成64位的Dll. 用外面的libQ ...