当前位置: 移动技术网 > IT编程>开发语言>.net > (23)ASP.NET Core EF关系数据库建模

(23)ASP.NET Core EF关系数据库建模

2019年11月05日  | 移动技术网IT编程  | 我要评论

野人时代,cad教程我要自学网,超级机械帝国txt

1.简介

一般而言,本部分中的配置适用于关系数据库。安装关系数据库提供程序时,此处显示的变为可用扩展方法(原因在于共享的microsoft.entityframeworkcore.relational包)。

2.表映射

表映射标识在数据库中哪张表应该进行内容查询和保存操作。

2.1约定

按照约定,每个实体将设置为映射到名称与dbset<tentity> 属性(公开派生上下文中的实体)相同的表中。如果给定dbset<tentity>实体中不包含,则使用类名称。

2.2数据注释

可以使用数据注释来配置类型映射表。

[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; }
}

2.3fluent api

你可以使用熟知的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");

3.列映射

列映射标识在数据库中应从哪些列数据中进行查询和保存。

3.1约定

按照约定,每个属性将会设置为映射到与属性具有相同名称的列。

3.2数据注释

可以使用数据注释来配置属性映射到的那一列。

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; }
    }
}

3.3fluent api

您可以使用熟知的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; }
    }
}

4.数据类型

数据类型是指属性所映射到的列的数据库特定类型。

4.1约定

按照约定,数据库提供程序基于属性的.net类型选择数据类型。它还会考虑其他元数据,如配置的最大长度、属性是否是主键的一部分等。例如,sql server的datetime、nvarchar(max) 用作键的属性。

4.2数据注释

您可以使用数据注释为列指定精确的数据类型。例如,下面的代码将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; }
}

4.3fluent api

你还可以使用熟知的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; }
}

5.主键

为每个实体类型的键引入primary key(主键)约束。

5.1约定

按照约定,会将数据库中的主键命名为pk_<type name>。

5.2数据注释

不能使用数据批注配置主键的关系数据库的特定方面。

5.3fluent api

你可以使用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; }
}

6.默认架构

如果没有为该对象显式配置架构,则默认架构为将在其中创建对象的数据库架构。

6.1约定

按照约定,数据库提供程序将选择最适合的默认架构。例如,microsoft sql server将使用dbo架构,而且sqlite将不使用架构(因为sqlite不支持架构)。

6.2数据注释

不能使用数据批注设置默认架构。

6.3fluent api

可以使用api来指定默认架构。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.hasdefaultschema("blogging");
    }
}

7.默认值

如果插入新行,但没有为该列指定值,则列的默认值为要插入的值。

7.1约定

按照约定,未配置默认值。

7.2数据注释

不能使用数据批注设置默认值。

7.3fluent api

你可以使用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; }
}

8.索引(关系数据库)

关系数据库中的索引映射到与实体框架核心中的索引相同的概念。

8.1约定

按照约定,索引命名为ix_<type name>_<property name>。对于复合索引<property name>,将成为以下划线分隔的属性名称列表。

8.2数据注释

不能使用数据批注配置索引。

8.3fluent api

你可以使用熟知的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; }
}


参考文献:






如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网