原文地址:[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. Hack技术

    Hack技术 1.IE条件注释法,微软官方推荐的hack方式. 只在IE下生效 <!--[if IE]> <link rel="stylesheet" href= ...

  2. 关于viewpoint的疑惑

    问题: 为什么在手机上打开一个PC web页面,用手机打开一个宽度为980的固定布局页面,页面会默认缩放到刚好满屏显示,并不会出现横向滚动条? 一:设备像素和CSS像素区别 现代浏览器中实现缩放的方式 ...

  3. 关于 QRCode 的问题[C# 生成二维码固定大小]

    一直在纠结了一天多的问题:就是为什么生成的二维码会随着内容多少的变化而变化大小,但是为什么网上做出来的二维码内容变化但是大小却没有变化?刚开始的时候我就一直围绕着是不是根据缩略图来做的,但是这个方法行 ...

  4. CCLabelAtlas

    1.CCLabelAtlas 的具体用法 CCLabelAtlas *pLabel = new CCLabelAtlas;pLabel ->initWithString("0123&q ...

  5. Excel报表开发

    读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...

  6. 解决在web项目使用log4j中无法将log信息写入文件

    这是log4j.properties中关于的配置 log4j.appender.appender2.File=F:/myeclipseworkspace2/SecondBook2/log/second ...

  7. node exports与 module.exports的区别

    你肯定非常熟悉nodejs模块中的exports对象,你可以用它创建你的模块.例如:(假设这是rocker.js文件) exports.name = function() { console.log( ...

  8. Run P4 without P4factory - A Simple Example In Tutorials. -2 附 simple_router源码

    /* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...

  9. C#winform调用外部程序,等待外部程序执行完毕才执行下面代码

    1.简单调用外部程序文件(exe文件,批处理等),只需下面一行代码即可 System.Diagnostics.Process.Start(“应用程序文件全路径”); 2.如果要等待调用外部程序执行完毕 ...

  10. iOS 用collectionview 做的无限图片滚动 广告banner适用

    使用方法见demo,bug未知,若有什么问题欢迎留言:) http://files.cnblogs.com/files/n1ckyxu/NickyScrollImageView.zip demo使用s ...