创建并配置模型Creating and configuring a model

10/13/2020

本文内容

Entity Framework 使用一组约定基于实体类的形状构建模型。Entity Framework uses a set of conventions to build a model based on the shape of your entity classes. 可指定其他配置以补充和/或替代约定的内容。You can specify additional configuration to supplement and/or override what was discovered by convention.

本文介绍可应用于面向任何数据存储的模型的配置,以及面向任意关系数据库时可应用的配置。This article covers configuration that can be applied to a model targeting any data store and that which can be applied when targeting any relational database. 提供程序还可支持特定于具体数据存储的配置。Providers may also enable configuration that is specific to a particular data store. 有关提供程序特定配置的文档,请参阅数据库提供程序部分。For documentation on provider specific configuration see the Database Providers section.

提示

可在 GitHub 上查看此文章的示例。You can view this article’s sample on GitHub.

使用 fluent API 配置模型Use fluent API to configure a model

可在派生上下文中替代 OnModelCreating 方法,并使用 ModelBuilder API 来配置模型。You can override the OnModelCreating method in your derived context and use the ModelBuilder API to configure your model. 此配置方法最为有效,并可在不修改实体类的情况下指定配置。This is the most powerful method of configuration and allows configuration to be specified without modifying your entity classes. Fluent API 配置具有最高优先级,并将替代约定和数据注释。Fluent API configuration has the highest precedence and will override conventions and data annotations.

using Microsoft.EntityFrameworkCore;

namespace EFModeling.FluentAPI.Required

{

class MyContext : DbContext

{

public DbSet Blogs { get; set; }

#region Required

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

modelBuilder.Entity()

.Property(b => b.Url)

.IsRequired();

}

#endregion

}

public class Blog

{

public int BlogId { get; set; }

public string Url { get; set; }

}

}

分组配置Grouping configuration

To reduce the size of the OnModelCreating method all configuration for an entity type can be extracted to a separate class implementing IEntityTypeConfiguration.

public class BlogEntityTypeConfiguration : IEntityTypeConfiguration

{

public void Configure(EntityTypeBuilder builder)

{

builder

.Property(b => b.Url)

.IsRequired();

}

}

然后,只需从 OnModelCreating 调用 Configure 方法。Then just invoke the Configure method from OnModelCreating.

new BlogEntityTypeConfiguration().Configure(modelBuilder.Entity());

可以在给定程序集中应用实现 IEntityTypeConfiguration 的类型中指定的所有配置。It is possible to apply all configuration specified in types implementing IEntityTypeConfiguration in a given assembly.

modelBuilder.ApplyConfigurationsFromAssembly(typeof(BlogEntityTypeConfiguration).Assembly);

备注

应用配置的顺序是不确定的,因此仅当顺序不重要时才应使用此方法。The order in which the configurations will be applied is undefined, therefore this method should only be used when the order doesn't matter.

使用数据注释来配置模型Use data annotations to configure a model

也可将特性(称为数据注释)应用于类和属性。You can also apply attributes (known as Data Annotations) to your classes and properties. 数据注释会替代约定,但会被 Fluent API 配置替代。Data annotations will override conventions, but will be overridden by Fluent API configuration.

using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations;

namespace EFModeling.DataAnnotations.Required

{

class MyContext : DbContext

{

public DbSet Blogs { get; set; }

}

#region Required

public class Blog

{

public int BlogId { get; set; }

[Required]

public string Url { get; set; }

}

#endregion

}

efcore 实体配置_创建并配置模型相关推荐

  1. 路由器snmp配置_基于keepalived配置数据库主从实现高可用

    基于keepalived配置数据库主从实现高可用 使用keepalived来监听端口,实现数据库的高可用.实现效果,其中一台数据库服务器突然出故障或关机时,应该不影响应用正常运行,等待服务器启动之后, ...

  2. jax-rs配置_具有MicroProfile配置的可配置JAX-RS ExceptionMapper

    jax-rs配置 当使用JAX-RS创建REST服务时,通常不返回任何内容(例如HTTP 201/2/4等)或某些数据(可能采用JSON格式(因此HTTP 200),或者返回某些异常/错误(因此HTT ...

  3. 西农 生成树配置_华为交换机配置STP功能示例

    配置STP功能示例 STP简介 以太网交换网络中为了进行链路备份,提高网络可靠性,通常会使用冗余链路.但是使用冗余链路会在交换网络上产生环路,引发广播风暴以及MAC地址表不稳定等故障现象,从而导致用户 ...

  4. EF CodeFirst 如何通过配置自动创建数据库当模型改变时

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...

  5. snippets vscode 配置_创建自己的Code Snippets在VSCode中

    创建自己的Code Snippets在VSCode中 创建Vuejs文件模板代码片段 1. Go to Code → Preferences → User Snippets 2.弹出提示框,选择一个v ...

  6. vscode python环境配置_用VScode配置Python开发环境

    前言 VScode是一个相当优秀的IDE,具备开源.跨平台.模块化.插件丰富.启动时间快.颜值高.可高度定制等等优秀的特质,不愧是微软爸爸的私生子. 所以用VScode来编写Python,也是相当的好 ...

  7. nginx nodejs环境配置_服务器环境配置安装(mysql+redis+nodejs+nginx)

    公司用来测试的服务器挂了,最后重装了系统,需要重新配置程序运行环境,linux上安装不是很熟悉,特此记录一下. 首先获取系统版本信息: 一.mysql 1. 安装 安装命令:sudo apt-get ...

  8. AndroidStudio_在android中使用properties配置文件_进行配置_只能读取配置_不能写入配置_放在assets---Android原生开发工作笔记230

    很简单了,直接说怎么用,到时候copy过去就用吧. 首先在,项目的对应的src目录下的,main文件夹下,创建一个assets文件夹 然后,在文件夹中创建一个config.properties文件 然 ...

  9. vim 配置_一步步配置VIM(3)-与latex结合

    今天看到那个外国小哥latex和vim玩的如此666,特别的模仿了一下.将过程记录下来,有些收获,也有些问题,留待备忘. 环境 Winows 10中使用hyper-v搭建虚拟机,来宾机是ubuntu ...

最新文章

  1. 小白学python买什么书-小白如何高效率学习python?真心建议(附教程)
  2. 快速撑握C#知识点之变量,类型及类型转换
  3. mysql乐观和悲观锁实现_mysql实现乐观锁和悲观锁该怎么编写?
  4. phpstorm配置Xdebug
  5. 机械臂中的四元素转为旋转矩阵_雅克比矩阵(上)雅克比推导
  6. 存储系统又慢又杂太难用?我们推荐这几本书帮你解决问题
  7. python运行界面黑色,在Python中使用open执行轮廓检测后,如何使图像的背景变黑?...
  8. 红安一中高考2021成绩查询,红安一中2019高考喜报成绩、一本二本上线人数情况...
  9. 基于hadoop的气象数据可视化分析
  10. 全国省市区县数据库(大全)
  11. Syntax Error Error No ESLint configuration found in statusGitmibsrc
  12. cfn-signal
  13. tws蓝牙耳机哪个牌子好?2022蓝牙耳机排行榜
  14. 无线网络技术—学习AC的基础配置
  15. 自然语言处理顶会 NAACL 2018 最佳论文、时间检验论文揭晓
  16. 有效性指标——传输速率和频带利用率
  17. 机器学习笔记 - 时间序列使用机器学习进行预测
  18. Brave Game ——巴什博奕
  19. 我的见解之hibernate(八)
  20. 杭州python培训价格

热门文章

  1. SQLServer重建索引
  2. Android为网络请求自定义加载动画
  3. php deprecated,解决php中each函数在7.2版本提示deprecated错误问题
  4. scp会覆盖同名文件吗_你会Hypermesh一键式完成几何文件到求解文件的输出吗?
  5. 移植wpa_supplicant软件与DHCP软件解密WPA/WPA2 加密的无线网络
  6. 线程高级篇-Lock锁和Condition条件
  7. 李春雷 | 夜宿棚花村
  8. selenium+python自动化80-文件下载(不弹询问框)
  9. Ubuntu 16.04 设置MySQL远程访问权限
  10. 自动编译失效的Oracle数据库对象