efcore 实体配置_创建并配置模型

创建并配置模型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

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/431246.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python怎么放音乐_python怎么播放音乐

Python播放音乐要使用pygame模块 首先安装pygame模块:pip install pygame 方法实现:def play_music(self): filepath r"E:\music\消愁.mp3"; pygame.mixer.init() # 加载音乐 pygame.mixer.music.load(filepath) pygame.mixer.music.play(sta…

sprintf_s与_snprintf与_snprintf_s

MSDN页面分别如下: spirntf_s: http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx _snprintf: http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28vVS.90%29.aspx _snprintf_s: http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx 三个页面…

git ssh配置文件 服务器_git登录ssh服务器

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":6,"count":6}]},"card":[{"des":"云服务器 ECS(Elastic Compute Service)是一…

怎么用计算机打分数,电脑excel中分数怎么打出来(图解excel分数输入法)

在日常工作中,我们经常需要在excel中输入分数,默认情况下,当我们直接输入分数,比如输入“9/16” 回车后,会发现系统显示的却是日期数据,如下图所示,那么分数该怎么输入呢?今天就来和…

python终止线程_Python里怎么终止一个线程

Python用sleep停止一个线程的运行,而不影响主线程的运行,案例代码如下: from threading import *import timeclass MyThread(Thread): def run (self): self.ifdo True; while self.ifdo: print I am running... time.sleep(2) def stop (se…

C#中的多线程

1简介及概念 C# 支持通过多线程并行执行代码,线程有其独立的执行路径,能够与其它线程同时执行。 一个 C# 客户端程序(Console 命令行、WPF 以及 Windows Forms)开始于一个单线程,这个线程(也称为“主线程”…

word 7桌面上的计算机图标是,怎么设置win7系统桌面图标都变成word图标的处理办法...

随着电脑的使用率越来越高,我们有时候可能会遇到win7系统桌面图标都变成word图标问题,如果我们遇到了win7系统桌面图标都变成word图标的问题,要怎么处理win7系统桌面图标都变成word图标呢?我们只用  双击打开桌面上的计算机文件…

edge浏览器什么相当于ie的中低_Microsoft Edge浏览器

Microsoft Edge浏览器是微软与Win10同步推出的一款新版浏览器。Microsoft Edge浏览器功能很全面,不仅内置微软Contana,可以为用户带来更多人性化的服务,而且Microsoft Edge浏览器还有着支持插件扩展、网页阅读注释等特色功能,为用…

python3文件的编码类型是什么_python3.x - 怎么查看某个文件的编码方式?python

问 题 python的确很赞,但是编码这个问题真的太尿性了。。。 现在我有一个文件,不论他的编码方式是什么,我都想把它转化成utf-8的格式。我的想法是先读取文件内容,保存到data变量中去,然后再以utf-8的格式写入文件。 但…

STL中 map 和 multimap

1. 所在头文件<map>. 命名空间std, 声明如下: 1 namespace std{2 template <class Key,class T,3 class Compare less<Key>,4 class Allocator allocator<pair<const Key,T> > >5 class map;6 templa…

你每隔多久使用计算机上网查找资料英文,牛津英语8B Unit3导学案

43. Each of the students should ___________(treat) equally[平等地] by our teachers.44. ---When _______ the Museum of London ______(build), do you know? ---Sorry, I‘ve no idea.45.This picture ___________ (paint) by Millie yesterday.46. These photos ______…

python源码剖析_Python源码剖析 - 对象初探

01 前言 对象是 python 中最核心的一个概念&#xff0c;在python的世界中&#xff0c;一切都是对象&#xff0c;整数、字符串、甚至类型、整数类型、字符串类型&#xff0c;都是对象。 02 什么是PyObject Python 中凡事皆对象&#xff0c;而其中 PyObject 又是所有对象的基础&a…

html点线面制作,利用HTML5绘制点线面组成的3D图形的示例

玩Canvas玩了有两三个礼拜了&#xff0c;平面的东西玩来玩去也就那样&#xff0c;所以就开始折腾3D了。因为Canvas画布终究还是平面的&#xff0c;所以要有3D就得抽象出一个Z轴。然后再把3D坐标转换成2D坐标&#xff0c;画到画布上&#xff0c;再通过旋转等变换效果来产生3D感。…

javapanel根据内部组件_[译]避免在unmounted组件上调用setState

[译]避免在unmounted组件上调用setState原文 : https://www.robinwieruch.de/react-warning-cant-call-setstate-on-an-unmounted-component很多人在开发 React 的过程中&#xff0c;会遇到下面这些警告。github上很多issue都和这些警告相关。因此&#xff0c;我想在这篇文章里…

如何写一个脚本语言_零基础小白如何学会写文案?文案写作技巧之一:如何写一个吸引读者的文案开头...

我文笔不好怎样才能够写出优质的文案&#xff1f;很多人都有这个疑问&#xff0c;包括我自己在学习新媒体运营之前&#xff0c;这也是我最大的困惑。现在是内容为王的时代&#xff0c;你的文章质量决定了你的KPI。后来我学习了文章的写作结构和技巧后&#xff0c;就能够写出一篇…

Extjs之RowNumberer

Ext.grid.RowNumberer,Ext.grid.PageRowNumberer 在一个数据表格中,一般都会加一个行号,在ExtJs中,要实现行号这一效果,只需要一句代码: new Ext.grid.RowNumberer() 举个例子,代码如下: var colModel new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),{ header: "…

计算机硬件的维护知识,电脑放了一年开不了机 电脑硬件维护常识要点有哪些...

随着经济的发展&#xff0c;人们走到哪里都是拿着手机&#xff0c;对于家里已有电脑的&#xff0c;完全不会去用下&#xff0c;很多人都觉得手机更加方便&#xff0c;但长时间不用电脑就会导致电脑当中的一些设备出现问题&#xff0c;都知道电脑它主要就会由一些硬件与软件进行…

发那科pmc地址分配_一台全新的FANUC数控机床,请简述有挡块回参功能的实现步骤?包括PMC的I/O分配、具体参数设定、梯形图程序...

展开全部挡块回636f70793231313335323631343130323136353331333433623063参功能的实现步骤由x地址决定的&#xff1a;1、打到回零的选项,动轴回零。2、当挡块被轴台压到的时候就减速了,挡块被压的时候PMC的X地址有一个会变成0。PMC的I/O分配&#xff1a;早期的I/O板用于数控系统…

计算机学院寝室文明风景线活动,小猿关注 | 营造良好学风 打造和谐宿舍 ——计算机学院开展学风主题教育暨文明宿舍评选活动...

原标题&#xff1a;小猿关注 | 营造良好学风 打造和谐宿舍 ——计算机学院开展学风主题教育暨文明宿舍评选活动计算机学院记者团讯(通讯员 苏婉静 编辑 王鸿宇)12月15日晚&#xff0c;计算机学院于九号楼9201教室开展“ 营造良好学风&#xff0c;打造和谐宿舍”主题教育活动&am…

c++ 结构体初始化_STM32入门系列-使用库函数点亮LED,LED初始化函数

要点亮LED&#xff0c;需要完成LED的驱动&#xff0c; 在工程模板上新建一个led.c和led.h文件&#xff0c;将其存放在led文件夹内。这两个文件需要我们自己编写。 通常xxx.c文件用于存放编写的驱动程序&#xff0c;xxx.h文件用于存放xxx.c内的stm32头文件、管脚定义、全局变量声…