原文:http://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspx

本节,我们将学习如何使用Fluent API配置实体类的属性。 我们将使用我们学校app的Student和Standard域类:

public class Student
{
public Student()
{ }
public int StudentKey { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; } public Standard Standard { get; set; }
} public class Standard
{
public Standard()
{ }
public int StandardKey { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; }
}

配置主键和复合主键

上面的域类,没有按照主键的Code-First约定,因为它们没有Id或{Class Name} + Id属性。

因此,你可以使用Fluent API使用EntityTypeConfiguration的HasKey()方法配置key属性,如下所示。 记住modelBuilder.Entity <TEntity>()返回EntityTypeConfiguration对象。

public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure primary key
modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey<int>(s => s.StandardKey); //Configure composite primary key
modelBuilder.Entity<Student>().HasKey<int>(s => new { s.StudentKey, s.StudentName });
}
}

配置列名、列类型和排序

默认Code-First约定为名属性创建一个同名的列名、顺序和数据类型的列。 你可以覆盖此约定,如下所示:

public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure Column
modelBuilder.Entity<Student>()
.Property(p => p.DateOfBirth)
.HasColumnName("DoB")
.HasColumnOrder()
.HasColumnType("datetime2");
}
}

如上例所示,我们使用Property()方法为实体的属性配置任何东西。

在这里,我们使用HasColumnName来更改DateOfBirth属性的列名。 此外,我们调用HasColumnOrder和HasColumnType来更改列的顺序和数据类型。

modelBuilder.Entity <TEntity>().Property(expression)允许您使用不同的方法配置特定属性,如下所示:

为属性配置Null或Not Null列

Code-First将为原始数据类型属性创建NotNull列,因为原始数据类型不能为空,除非使用?符号或Nullable <T>。

使用IsOptional方法为属性创建一个可空的列。

以同样的方式,使用IsRequired方法创建一个NotNull列。

namespace CodeFirst_FluentAPI_Tutorials
{ public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure Null Column
modelBuilder.Entity<Student>()
.Property(p => p.Heigth)
.IsOptional(); //Configure NotNull Column
modelBuilder.Entity<Student>()
.Property(p => p.Weight)
.IsRequired();
}
}
}

配置列大小

Code-First将设置列的数据类型的最大大小。 您可以覆盖此约定,如下所示:

namespace CodeFirst_FluentAPI_Tutorials
{ public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set StudentName column size to 50
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(); //Set StudentName column size to 50 and change datatype to nchar
//IsFixedLength() change datatype from nvarchar to nchar
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength().IsFixedLength(); //Set size decimal(2,2)
modelBuilder.Entity<Student>()
.Property(p => p.Height)
.HasPrecision(, );
}
}
}

如上例所示,我们使用HasMaxLength方法设置列的大小。

IsFixedLength方法将nvarchar转换为nchar类型。

以同样的方式,HasPrecision方法改变了十进制列的精度。

配置并发列

你可以使用ConcurrencyToken方法将属性配置为并发列,如下所示:

namespace CodeFirst_FluentAPI_Tutorials
{ public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set StudentName as concurrency column
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.IsConcurrencyToken();
}
}
}

如上例所示,我们将StudentName列设置为并发列,以便它被包含在update和delete命令的where子句中。

您也可以使用IsRowVersion()方法来将byte []属性作为并发列。

【译】第24节---Fluent API - 属性映射的更多相关文章

  1. 【译】第23节---Fluent API - 实体映射

    原文:http://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspx ...

  2. 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)

    使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...

  3. 使用Fluent API 配置/映射属性和类型

    Code First约定-Fluent API配置 使用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Code F ...

  4. 使用 Fluent API 配置/映射属性和类型

    使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...

  5. 【译】第22节---Fluent API - EntityTypeConfiguration类

    原文:http://www.entityframeworktutorial.net/code-first/entitytypeconfiguration-class.aspx 在我们开始使用Fluen ...

  6. 【译】第21节---Fluent API

    原文:http://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx 在前面的学习中.我们已经看到不同的 ...

  7. 使用 Fluent API 配置/映射属性和类型2

    1.将多个实体类映射到数据库中的一个表 要将多个实体映射到一个数据库表需要满足: a. 两个实体必须是一对一关系 b.两个实体共享一个主键 public class MyContext:DbConte ...

  8. EF使用Fluent API配置映射关系

    定义一个继承自EntityTypeConfiguration<>泛型类的类来定义domain中每个类的数据库配置,在这个自定义类的构造函数中使用我们上次提到的那些方法配置数据库的映射. 映 ...

  9. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

随机推荐

  1. 数据集成工具Kettle、Sqoop、DataX的比较

    数据集成工具很多,下面是几个使用比较多的开源工具. 1.阿里开源软件:DataX         DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).H ...

  2. css实现16:9的图片比例

    摘自:https://www.cnblogs.com/caizhenbo/p/css.html 需求: 最近产品要求不管原图的大小是多少,宽度一定,高度要自自适应为16:9. 分析: 对于正常的固定好 ...

  3. K8S学习笔记之修改K8S的api-server证书

    K8S的api-server证书包含很多IP和域名,有时候后期才发现证书内有错误,需要重新生成该证书. 修改server-csr.json,修改后基于原来的ca证书重新生成server.perm  s ...

  4. Golang生成区间随机整数

    package main import ( "fmt" "math/rand" "time" ) func main() { rand.Se ...

  5. Cannot resolve reference to bean 'sqlSessionFactory' while setting bean

    今天下载公司以前的一个项目,要和现在的项目进行整合,下载一切顺利,但在开发机器上一跑,憋了. 有两种日志,两种报错:一个是cannot resolve reference to bean 'sqlSe ...

  6. spring是如何控制事务

    1.spring的核心是ioc和aop,其中ioc是将控制权交由spring容器进行管理,aop是面向切面编程,内部实现使用的是动态代理,二动态代理内部实现用的是反射.spring的事务是通过aop来 ...

  7. 关于actor模型

    actor model是1973年就提出的一个分布式并发编程模型,在erlang语言中得到广泛支持和应用.目前Java中也出现了很多支持actor模型的库:akka.killim.jetlang等等, ...

  8. rman备份例子

    1.全备份例子 #!/bin/sh RMAN_OUTPUT_LOG=/home/oracle/rman_output.logRMAN_ERROR_LOG=/home/oracle/rman_error ...

  9. json排序 及替换在字符串中全部替换某字符串

    var roadLine = '@ViewBag.RoadLine'; var jsonRoadLine = JSON.parse(roadLine.replace(/"/g, '\&quo ...

  10. Eclipse中已安装的插件如何卸载

    最近在Eclipse中安装了一个插件,导致Eclipse使用的时候有些问题,就找了资料,原来Eclipse中的插件也是可以卸载的. 方法是点击菜单“Help”,“Install New Softwar ...