Entity Framework(七):Fluent API配置案例
一、配置主键
要显式将某个属性设置为主键,可使用 HasKey 方法。在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键。
1、新加Product类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Model
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
}
}
2、新建ProductMap类,用来设置主键
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using FluentAPI.Model; namespace FluentAPI.Data.FluentAPIMap
{
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
//使用 HasKey 方法对 Product 类型配置 ProductId 主键。
this.HasKey(p => p.ProductId);
}
}
}
3、查看数据库

二、配置复合主键
以下示例配置要作为Department 类型的组合主键的DepartmentID 和 Name 属性。
1、创建Department类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Model
{
public class Department
{
public int DepartmentId { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
}
}
2、创建DepartmentMap类,用来设置复合主键
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class DepartmentMap : EntityTypeConfiguration<Department>
{
public DepartmentMap()
{
// 使用匿名类的方式配置DepartmentId和Name作为复合主键
this.HasKey(p => new {p .DepartmentId,p.Name});
}
}
}
3、查看数据库
使用EF的数据迁移,然后查看数据库表

三、关闭数值主键的标识
数值主键的标识DatabaseGeneratedOption是一个枚举值,该枚举值具有下面三个值:
DatabaseGeneratedOption.None:关闭数值主键。
DatabaseGeneratedOption.Identity:设置数值主键 自动增长 ,
DatabaseGeneratedOption.Computed :数值主键的值由计算得到(此列将无法插入值)。
1、设置关闭数值主键
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class DepartmentMap : EntityTypeConfiguration<Department>
{
public DepartmentMap()
{
// 使用匿名类的方式配置DepartmentId和Name作为复合主键
this.HasKey(p => new {p .DepartmentId,p.Name}); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。
this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
}
插入数据库表的时候DepartmentId列要显示的指定值:
INSERT INTO Departments VALUES (1, '人事部',12.3,GETDATE());
四、指定属性的最大长度
HasMaxLength可以设置表中列的最大长度。
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class DepartmentMap : EntityTypeConfiguration<Department>
{
public DepartmentMap()
{
// 使用匿名类的方式配置DepartmentId和Name作为复合主键
this.HasKey(p => new {p .DepartmentId,p.Name}); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。
//如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。
this.Property(p => p.Name).HasMaxLength(); }
}
}
五、将属性配置为必需
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class DepartmentMap : EntityTypeConfiguration<Department>
{
public DepartmentMap()
{
// 使用匿名类的方式配置DepartmentId和Name作为复合主键
this.HasKey(p => new {p .DepartmentId,p.Name}); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。
//如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。
this.Property(p => p.Name).HasMaxLength(); /*
Name属性是必需的。如果不指定 Name,则出现 DbEntityValidationException 异常。如果 Code First 基于此模型创建数据库,则用于存储此属性的列将不可为空。
*/
this.Property(p => p.Name).IsRequired(); }
}
}
六、指定不将CLR 属性映射到数据库中的列
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class DepartmentMap : EntityTypeConfiguration<Department>
{
public DepartmentMap()
{
// 使用匿名类的方式配置DepartmentId和Name作为复合主键
this.HasKey(p => new {p .DepartmentId,p.Name}); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值由数据库自动生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); //Name属性不应超过 50 个字符。如果其值超过 50 个字符,则出现 DbEntityValidationException 异常。
//如果 Code First 基于此模型创建数据库,它还会将 Name 列的最大长度设置为50 个字符。
this.Property(p => p.Name).HasMaxLength(); /*
Name属性是必需的。如果不指定 Name,则出现 DbEntityValidationException 异常。如果 Code First 基于此模型创建数据库,则用于存储此属性的列将不可为空。
*/
this.Property(p => p.Name).IsRequired(); /*
以下示例显示如何指定CLR 类型的属性不映射到数据库中的列。
Ignore 等同于数据注解NotMapped
*/
this.Ignore(p => p.Budget); }
}
}
七、将CLR 属性映射到数据库中的特定列
HasColumnName可以用来设置映射到数据库表中列的列名。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using FluentAPI.Model;
using System.ComponentModel.DataAnnotations.Schema; namespace FluentAPI.Data.FluentAPIMap
{
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
//使用 HasKey 方法对 Product 类型配置 ProductId 主键。
this.HasKey(p => p.ProductId); /*
* 以下示例将Price CLR 属性映射到ProductPrice 数据库列。
*/
this.Property(p => p.Price).HasColumnName("ProductPrice");
}
}
}
八、配置字符串属性是否支持Unicode 内容
IsUnicode()方法可以用来设置是否支持Unicode字符,该方法有两个重载函数。
1、没有参数的重载,默认支持Unicode字符

2、有参数的重载,参数为bool值,true支持Unicode,false不支持Unicode

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using FluentAPI.Model;
using System.ComponentModel.DataAnnotations.Schema; namespace FluentAPI.Data.FluentAPIMap
{
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
//使用 HasKey 方法对 Product 类型配置 ProductId 主键。
this.HasKey(p => p.ProductId); /*
* 以下示例将Price CLR 属性映射到ProductPrice 数据库列。
*/
this.Property(p => p.Price).HasColumnName("ProductPrice"); /*
* 默认情况下,字符串为Unicode(SQLServer 中的nvarchar)。您可以使用IsUnicode 方法指定字符串应为varchar 类型。
*/
this.Property(p => p.PlaceOfOrigin).IsUnicode(false);
}
}
}
查看数据库列类型:

九、配置数据库列的数据类型
HasColumnType 方法支持映射到相同基本类型的不同表示。
/*
HasColumnType 方法支持映射到相同基本类型的不同表示。使用此方法并不支持在运行时执行任何数据转换。
* 请注意,IsUnicode 是将列设置为 varchar 的首选方法,因为它与数据库无关。
*/
this.Property(p => p.Name).HasColumnType("varchar");
十、配置复杂类型的属性
1、新建类Course,里面有一个Department类型的属性:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPIApp.Model
{
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public virtual Department Department { get; set; }
}
}
using FluentAPI.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FluentAPI.Data.FluentAPIMap
{
public class CourseMap : EntityTypeConfiguration<Course>
{
public CourseMap()
{
/*可以使用点表示法访问复杂类型的属性。
设置Course类里面的Department属性的Name的最大长度是32
*/
this.Property(p => p.Department.Name).HasMaxLength();
}
}
}
十一、将CLR 实体类型映射到数据库中的特定表
/*Department 的所有属性都将映射到名为 t_ Department 的表中的列。*/
ToTable("t_Department");
/*您也可以这样指定架构名称:*/
ToTable("t_Department", "school");
代码地址:https://pan.baidu.com/s/1eR20fWe
Entity Framework(七):Fluent API配置案例的更多相关文章
- Entity Framework(Fluent API)
一.概述 Fluent API 可以理解为一种从POCO到数据库的映射约定,包括字段长度,类型,主外键等等,在EF Code First进行开发时候经常用到. 1.主键 modelBuilder.En ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- Entity Framework Code First (五)Fluent API - 配置关系 转载 https://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
I.EF里的默认映射 上篇文章演示的通过定义实体类就可以自动生成数据库,并且EF自动设置了数据库的主键.外键以及表名和字段的类型等,这就是EF里的默认映射.具体分为: 数据库映射:Code First ...
- 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)
使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...
- 使用 Fluent API 配置/映射属性和类型
使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...
- EF——默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射 02 (转)
EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射 I.EF里的默认映射 上篇文章演示的通过定义实体类就可以自动生成数据库,并且EF自动设置了数据库 ...
- EF的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
I.EF的默认映射 上节我们创建项目,通过定义实体类就可以自动生成数据库,并且EF帮我们自动设置了数据库的主键.外键以及表名和字段的类型等,这就是EF的默认映射.具体分为: 数据库映射:Code Fi ...
随机推荐
- 苹果开发——App内购以及验证store的收据(一)
原地址:http://zengwu3915.blog.163.com/blog/static/27834897201375105236580?suggestedreading 发了几天时间在网上折腾了 ...
- SQL server 2008里面通过sys.dm_exec_procedure_stats得到存储过程的执行信息--转
--转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/13/sql-server-2008-sys-dm-exec-procedure-stats. ...
- 【Linux】nl命令
用途 nl主要用于输出行号,但是与cat -n不太一样的是,可以自定义输出行号的样式 全称 nl的全称为:Number of Lines 参数 -b :指定行号指定的方式,主要有2种: -b a :表 ...
- 记一次CurrentDirectory导致的问题
现在项目里需要实现一个功能如下: A.exe把B.exe复制到临时目录,然后A.exe退出,B.exe负责把A.exe所在的整个目录删除. 实现: A.exe用CreateProcess创建B.exe ...
- iOS 3DTouch
概述 iOS10系统登录中国,在系统中对3D Touch的使用需求更频繁,所以对iOS9中便引入的3D Touch功能做一些了解是很有必要的 详细 代码下载:http://www.demodashi. ...
- 转载【微信支付】jsapi支付之传参问题(使用微信官方SDK之PHP版本) V3之WxpayPubHelper 亲测有效,V3WxpayAPI_php_v3.zip版未测试,理论上也是一样的。
本文转载至:http://blog.csdn.net/geeklx/article/details/51146151 (微信支付现在分为v2版和v3版,2014年9月10号之前申请的为v2版,之后申请 ...
- Linux索引节点(Inode)用满导致空间不足
一.问题出现 在创建新目录和文件是提示“no space left on device”!按照以前的情况,很有可能是服务器空间又被塞满了,通过命令查看,发现还有剩余.再用df -i查看了一下/分区的索 ...
- jquery 常用api 小结2
*一)jQuery常用方法API实战 (1)DOM简述与分类 A)DOM是一种标准,它独立于平台,语言,浏览器. B)如果项目中,你完全按照DOM标准写代码,你就能在各大主流的浏览器中操作标准控件. ...
- .Net Excel 导出图表Demo(柱状图,多标签页) .net工具类 分享一个简单的随机分红包的实现方式
.Net Excel 导出图表Demo(柱状图,多标签页) 1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一 ...
- springboot 1.5.10 +kotlin 1.2.20 解决 java.lang.ClassNotFoundException: kotlin.reflect.KotlinReflectionInternalError
使用http://start.spring.io/ 下载的 项目 跑单元测试 报 java.lang.ClassNotFoundException: kotlin.reflect.KotlinRefl ...