原文: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. JAVA 中的 Collection 和 Map 以及相关派生类的概念

    JAVA中Collection接口和Map接口的主要实现类   Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...

  2. 转:WCF传送二进制流数据基本实现步骤详解

    来自:http://developer.51cto.com/art/201002/185444.htm WCF传送二进制流数据基本实现步骤详解 2010-02-26 16:10 佚名 CSDN   W ...

  3. 动态创建生成lambd表达式

    基于网上找的一段代码进行修改,目前扩展了NotContains方法的实现 using System; using System.Collections.Generic; using System.Co ...

  4. 怎样从外网访问内网Tomcat?

    本地安装了一个Tomcat,只能在局域网内访问,怎样从外网也能访问到本地的Tomcat呢?本文将介绍具体的实现步骤. 准备工作 安装并启动Tomcat 默认安装的Tomcat端口是8080. 实现步骤 ...

  5. Tomcat启动程序端口冲突、确认相应进程及杀死冲突进程的解决方案

    一. 查看所有进程占用的端口 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 二.查看占用指定端口的程序(1)命令窗口输出 命令:netstat –ano | findstr & ...

  6. php操作共享内存shmop类及简单使用测试(代码)

    SimpleSHM 是一个较小的抽象层,用于使用 PHP 操作共享内存,支持以一种面向对象的方式轻松操作内存段.在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码.可以使用 3 ...

  7. rabbitmq队列的exclusive,durability,auto-delete属性以及消息可靠传输设计

    非集群下,简单的说:- 如果是excl,则设置durability没有意义,因为不管服务器挂了还是客户端主动/被动断开了,队列都会自动删除.- auto-delete,其实可简单的认为是同理,即使非e ...

  8. 对浏览器攻击:MS10-002

    对浏览器攻击:MS10-002 MS10-002漏洞介绍 针对微软Internet Explorer"极光"内存损坏的攻击,当用户查看特制网页时允许远程执行代码. 实践过程 命令行 ...

  9. PyCharm笔记之首次安装和激活

    转载:http://www.cnblogs.com/Ivyli4258/p/7440147.html PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其 ...

  10. 【Python57--正则1】

    一.正则表达式匹配IP地址 1.search()方法:用于在字符串中搜索正则表达式模式第一次出现的位置 >>> import re >>> re.search(r' ...