1.必需和可选属性

如果实体属性可以包含null,则将其视为可选。如果属性的有效值不可以包含null,则将其视为必需属性。映射到关系数据库架构时,必需的属性将创建为不可为null的列,而可选属性则创建为可以为null的列。

1.1约定

按照约定,.net 类型可以包含null的属性将配置为可选,而.net类型不包含null的属性将根据需要进行配置。例如,具有.net值类型(int、decimal、bool等)的所有属性都是必需的,而具有可为null的.net值类型(int?、decimal?、bool?等)的所有属性都是配置为可选。

1.2数据批注

可以按如下所示将”约定”可以为”可选”的属性配置为”必需”:

namespace efmodeling.dataannotations.required
{
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
    }
    public class blog
    {
        public int blogid { get; set; }
        //加上这个批注,这个值就必需写入
        [required]
        public string url { get; set; }
    }
}

1.3fluent api

namespace efmodeling.fluentapi.required
{
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<blog>()
                .property(b => b.url)
                //这个方法表示必需写入
                .isrequired();
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
    }
}

2.最大长度

配置最大长度可为数据存储提供有关要对给定属性使用的相应数据类型的提示。最大长度仅适用于数组数据类型,如string和byte[]。例如前端传统数据长度远大于限定的长度,则提示。

2.1约定

按照约定,应由数据库提供程序为属性选择适当的数据类型,即数据库字段设置长度多少,生产程序实体接受值时就限定长度多少。对于具有长度的属性,数据库提供程序通常将选择允许最长数据长度的数据类型。例如,microsoft sql server将对字符string属性使用 nvarchar(max)(如果该列用作键,则会使用nvarchar(450))。

2.2数据批注

你可以使用数据批注为属性配置最大长度。此示例面向sql server,因此使用数据类型 nvarchar(500)。

namespace efmodeling.dataannotations.maxlength
{
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
    }
    public class blog
    {
        public int blogid { get; set; }
        //设置最大长度
        [maxlength(500)]
        public string url { get; set; }
    }
}

2.3fluent api

namespace efmodeling.fluentapi.maxlength
{
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<blog>()
                .property(b => b.url)
                //设置最大长度
                .hasmaxlength(500);
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
    }
}

3.并发标记

当我们发现生产环境某个实体字段经常处于并发当中,我们可以批注一下为并发字段。

3.1约定

按照约定,属性永远不会配置为并发标记。

3.2数据注释

您可以使用数据批注将属性配置为并发标记。

public class person
{
   public int personid { get; set; }
    //并发标记
    [concurrencycheck]
    public string lastname { get; set; }
    public string firstname { get; set; }
}

3.3fluent api

您可以使用熟知的api将属性配置为并发标记。

class mycontext : dbcontext
{
    public dbset<person> people { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<person>()
            .property(p => p.lastname)
            //并发标记
            .isconcurrencytoken();
    }
}
public class person
{
    public int personid { get; set; }
    public string lastname { get; set; }
    public string firstname { get; set; }
}

4.时间戳/行版本

时间戳是一个属性类型,在每次插入或更新行时,数据库都会生成一个新值。此该属性类型也被视为并发标记。这可以确保在你和其他人修改了行数据时你会收到异常信息。

4.1约定

按照约定,属性永远不会配置为时间戳。

4.2数据注释

你可以使用数据批注将属性配置为时间戳。

public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    //设置时间戳
    [timestamp]
    public byte[] timestamp { get; set; }
}

4.3fluent api

你可以使用熟知的api将属性配置为时间戳。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<blog>()
            .property(p => p.timestamp)
            //设置时间戳
            .isrowversion();
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    public byte[] timestamp { get; set; }
}

5.阴影属性

当数据库中的数据不应在映射的实体类型上公开时,阴影属性非常有用。它们最常用于外键属性,其中两个实体之间的关系由数据库中的外键值表示,但使用实体类型之间的导航属性在实体类型上管理关系,可以通过changetracker api获取和更改影子属性值:

context.entry(myblog).property("lastupdated").currentvalue = datetime.now;

可以通过ef.property静态方法在linq查询中引用影子属性:

var blogs = context.blogs.orderby(b => ef.property<datetime>(b, "lastupdated"));

5.1约定

如果发现了关系,但在依赖实体类中找不到外键属性,则可以按约定创建阴影属性。在这种情况下,将引入阴影外键属性。影子外键属性将命名<navigation property name><principal key property name>为(指向主体实体的依赖实体上的导航用于命名)。如果主体键属性名称包含导航属性的名称,则该名称将只是<principal key property name>。如果依赖实体上没有导航属性,则会在其位置使用主体类型名称。
例如,下面的代码列表将导致blogid post向实体引入阴影属性。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    public dbset<post> posts { get; set; }
}
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; }
}

5.2数据注释

不能通过数据批注创建阴影属性。

5.3fluent api

你可以使用”熟知api”配置阴影属性。一旦你调用了property方法的字符串重载,就可以链接到其他属性的任何配置调用。如果提供property方法的名称与现有属性的名称相匹配(一个阴影属性或在实体类中定义的属性),则代码将配置该现有属性,而不是引入新的阴影属性。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<blog>()
            //创建阴影属性
            .property<datetime>("lastupdated");
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
}

参考文献:
必需和可选属性