Entity Framework使用Fluent API配置的方法
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,本篇内容介绍了"Entity Framework使用Fluent API配置的方法"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望
千家信息网最后更新 2025年11月09日Entity Framework使用Fluent API配置的方法
本篇内容介绍了"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 { 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 { 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 { 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); } }} 2、插入数据库表的时候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 { 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(50); } }} 五、将属性配置为必需
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 { 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(50); /* 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 { 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(50); /* 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 { 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 { 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 { public CourseMap() { /*可以使用点表示法访问复杂类型的属性。 设置Course类里面的Department属性的Name的最大长度是32 */ this.Property(p => p.Department.Name).HasMaxLength(32); } }} 十一、将CLR 实体类型映射到数据库中的特定表
/*Department 的所有属性都将映射到名为 t_ Department 的表中的列。*/ToTable("t_Department");/*您也可以这样指定架构名称:*/ToTable("t_Department", "school");"Entity Framework使用Fluent API配置的方法"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
数据
属性
数据库
配置
类型
字符
方法
示例
支持
指示
生成
最大
数值
长度
方式
模型
内容
参数
字符串
自动生成
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
百度互联网领先科技
华为通讯服务器
数据库一个数字占几位
手机的服务器怎么下载
软件开发刚毕业工资
阿里云服务器拒绝连接
三丰云免费服务器有什么用
服务器在那里
怎么把电脑代理服务器的地址删除
数据库的故障恢复
自学软件开发的步骤
华硕服务器机箱
京东数据库怎么修改
数据库用代码删除字段名称
网络安全规范要求网口封闭
怎么停止数据库运行
双阳区网络技术服务参考价格
宁波余姚市服务器维修
网络安全战略研究所所长
亚马逊软件开发包
湖北省计算机网络技术有限公司
写微信小程序时数据存在数据库
计算机网络技术专业学习规划
iptv服务器管理地址
速诺科技参加互联网大会
小残博客 关注网络安全
服务器开机后电脑桌面就没了
查询数据库记录条数的关键字
暗黑2重置服务器回档
数据库操作异常