野人时代,cad教程我要自学网,超级机械帝国txt
一般而言,本部分中的配置适用于关系数据库。安装关系数据库提供程序时,此处显示的变为可用扩展方法(原因在于共享的microsoft.entityframeworkcore.relational包)。
表映射标识在数据库中哪张表应该进行内容查询和保存操作。
按照约定,每个实体将设置为映射到名称与dbset<tentity> 属性(公开派生上下文中的实体)相同的表中。如果给定dbset<tentity>实体中不包含,则使用类名称。
可以使用数据注释来配置类型映射表。
[table("blogs")] public class blog { public int blogid { get; set; } public string url { get; set; } }
你还可以指定表所属的架构(数据库)。
[table("blogs", schema = "blogging")] public class blog { public int blogid { get; set; } public string url { get; set; } }
你可以使用熟知的api来配置类型映射到的表。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .totable("blogs"); } } public class blog { public int blogid { get; set; } public string url { get; set; } }
你还可以指定表所属的架构(数据库)。
modelbuilder.entity<blog>().totable("blogs", schema: "blogging");
列映射标识在数据库中应从哪些列数据中进行查询和保存。
按照约定,每个属性将会设置为映射到与属性具有相同名称的列。
可以使用数据注释来配置属性映射到的那一列。
namespace efmodeling.dataannotations.relational.column { class mycontext : dbcontext { public dbset<blog> blogs { get; set; } } public class blog { [column("blog_id")] public int blogid { get; set; } public string url { get; set; } } }
您可以使用熟知的api来配置属性映射到的列。
namespace efmodeling.fluentapi.relational.column { class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .property(b => b.blogid) .hascolumnname("blog_id"); } } public class blog { public int blogid { get; set; } public string url { get; set; } } }
数据类型是指属性所映射到的列的数据库特定类型。
按照约定,数据库提供程序基于属性的.net类型选择数据类型。它还会考虑其他元数据,如配置的最大长度、属性是否是主键的一部分等。例如,sql server的datetime、nvarchar(max) 用作键的属性。
您可以使用数据注释为列指定精确的数据类型。例如,下面的代码将url配置为一个非unicode字符串,其最大200长度。rating为5至2小数位。
public class blog { public int blogid { get; set; } [column(typename = "varchar(200)")] public string url { get; set; } [column(typename = "decimal(5, 2)")] public decimal rating { get; set; } }
你还可以使用熟知的api为列指定相同的数据类型。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>(eb => { eb.property(b => b.url).hascolumntype("varchar(200)"); eb.property(b => b.rating).hascolumntype("decimal(5, 2)"); }); } } public class blog { public int blogid { get; set; } public string url { get; set; } public decimal rating { get; set; } }
为每个实体类型的键引入primary key(主键)约束。
按照约定,会将数据库中的主键命名为pk_<type name>。
不能使用数据批注配置主键的关系数据库的特定方面。
你可以使用api在数据库中配置primary key(主键)约束的名称。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .haskey(b => b.blogid) .hasname("primarykey_blogid"); } } public class blog { public int blogid { get; set; } public string url { get; set; } }
如果没有为该对象显式配置架构,则默认架构为将在其中创建对象的数据库架构。
按照约定,数据库提供程序将选择最适合的默认架构。例如,microsoft sql server将使用dbo架构,而且sqlite将不使用架构(因为sqlite不支持架构)。
不能使用数据批注设置默认架构。
可以使用api来指定默认架构。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.hasdefaultschema("blogging"); } }
如果插入新行,但没有为该列指定值,则列的默认值为要插入的值。
按照约定,未配置默认值。
不能使用数据批注设置默认值。
你可以使用api指定属性的默认值。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .property(b => b.rating) .hasdefaultvalue(3); } } public class blog { public int blogid { get; set; } public string url { get; set; } public int rating { get; set; } }
还可以指定用于计算默认值的sql片段。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .property(b => b.created) .hasdefaultvaluesql("getdate()"); } } public class blog { public int blogid { get; set; } public string url { get; set; } public datetime created { get; set; } }
关系数据库中的索引映射到与实体框架核心中的索引相同的概念。
按照约定,索引命名为ix_<type name>_<property name>。对于复合索引<property name>,将成为以下划线分隔的属性名称列表。
不能使用数据批注配置索引。
你可以使用熟知的api来配置索引的名称。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .hasindex(b => b.url) .hasname("index_url"); } } public class blog { public int blogid { get; set; } public string url { get; set; } }
你还可以指定筛选器。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .hasindex(b => b.url) .hasfilter("[url] is not null"); } } public class blog { public int blogid { get; set; } public string url { get; set; } }
使用sql server提供程序ef为唯一索引中包含的所有可以为null的列添加"is not null"筛选器。若要重写此约定,可以null提供一个值。
class mycontext : dbcontext { public dbset<blog> blogs { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<blog>() .hasindex(b => b.url) .isunique() .hasfilter(null); } } public class blog { public int blogid { get; set; } public string url { get; set; } }
在sql server索引中包含列,当查询中的所有列都作为键列或非键列包含在索引中时,可以通过包含列配置索引以显著提高查询性能。
class mycontext : dbcontext { public dbset<post> posts { get; set; } protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<post>() .hasindex(p => p.url) .includeproperties(p => new { p.title, p.publishedon }) .hasname("index_url_include_title_publishedon"); } } public class post { public int postid { get; set; } public string url { get; set; } public string title { get; set; } public datetime publishedon { get; set; } }
参考文献:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论