EntityFramework Core 学习笔记 —— 包含与排除类型
原文地址:[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 学习笔记 —— 包含与排除类型的更多相关文章
- EntityFramework Core 学习笔记 —— 包含与排除属性
原文地址:https://docs.efproject.net/en/latest/modeling/included-properties.html 在模型中包含一个属性意味着 EF 拥有了这个属性 ...
- EntityFramework Core 学习笔记 —— 创建模型
原文地址:https://docs.efproject.net/en/latest/modeling/index.html 前言: EntityFramework 使用一系列的约定来从我们的实体类细节 ...
- EntityFramework Core 学习笔记 —— 添加主键约束
原文地址:https://docs.efproject.net/en/latest/modeling/keys.html Keys (primary) Key 是每个实体例的主要唯一标识.EF Cor ...
- EntityFramework Core 学习系列(一)Creating Model
EntityFramework Core 学习系列(一)Creating Model Getting Started 使用Command Line 来添加 Package dotnet add pa ...
- .NET CORE学习笔记系列(2)——依赖注入[7]: .NET Core DI框架[服务注册]
原文https://www.cnblogs.com/artech/p/net-core-di-07.html 包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IS ...
- .NET CORE学习笔记系列(2)——依赖注入[6]: .NET Core DI框架[编程体验]
原文https://www.cnblogs.com/artech/p/net-core-di-06.html 毫不夸张地说,整个ASP.NET Core框架是建立在一个依赖注入框架之上的,它在应用启动 ...
- .NET CORE学习笔记系列(2)——依赖注入[4]: 创建一个简易版的DI框架[上篇]
原文https://www.cnblogs.com/artech/p/net-core-di-04.html 本系列文章旨在剖析.NET Core的依赖注入框架的实现原理,到目前为止我们通过三篇文章从 ...
- .NET CORE学习笔记系列(2)——依赖注入【3】依赖注入模式
原文:https://www.cnblogs.com/artech/p/net-core-di-03.html IoC主要体现了这样一种设计思想:通过将一组通用流程的控制权从应用转移到框架中以实现对流 ...
- .NET CORE学习笔记系列(2)——依赖注入【1】控制反转IOC
原文:https://www.cnblogs.com/artech/p/net-core-di-01.html 一.流程控制的反转 IoC的全名Inverse of Control,翻译成中文就是“控 ...
随机推荐
- sprin加载顺序
spring加载有个比较有意思的问题,这里片很不错的文章 http://guoliangqi.iteye.com/blog/632697
- github上怎么预览页面
直接在 http://htmlpreview.github.io/? 后面加上git上的地址就可以预览了 比如 http://htmlpreview.github.io/?https://github ...
- bootstrap 不兼容ie8 的问题
官方推荐的脚手架中,其实已经包含着解决方案:html5shiv.min.js .Respond.min.js 但由于respond.js 使用 file:// 协议,IE8 是无法调起本地文件的 ...
- 网站部署后Parser Error Message: Could not load type 的解决方案
asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...
- BizTalk开发系列(十七) 信封架构(Envelop)
在BizTalk开过中使用信封架构可以提高BizTalk处理性能.比如在使用SQL Adapter时使用信封选取多条记录在通过管道的XML拆装器时将信封里的XML消息部分拆分为单独的消息,发布到Mes ...
- js官网判断是否手机跳转到手机页面
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="te ...
- 【转】C#(ASP.Net)获取当前路径的方法集合
转自:http://www.gaobo.info/read.php/660.htm //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Asse ...
- ThinkPHP 3.2.3 自动加载公共函数文件的方法
方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件 ...
- mac自定义安装nodejs步骤
自定义安装的好处是nodejs相关的文件都在同一个文件夹下,且不与其它程序的文件混合在同一文件夹下. 1.下载node并解压缩:https://nodejs.org/dist/,选择tar.gz包下载 ...
- 佛祖保佑 永无BUG 永不修改
// // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...