Entity?Framework使用Fluent?API配置案例
一、配置主鍵
要顯式將某個屬性設(shè)置為主鍵,可使用 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類,用來設(shè)置主鍵
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、查看數(shù)據(jù)庫

二、配置復(fù)合主鍵
以下示例配置要作為Department 類型的組合主鍵的DepartmentID 和 Name 屬性。
1、創(chuàng)建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、創(chuàng)建DepartmentMap類,用來設(shè)置復(fù)合主鍵
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作為復(fù)合主鍵
this.HasKey(p => new {p .DepartmentId,p.Name});
}
}
}3、查看數(shù)據(jù)庫
使用EF的數(shù)據(jù)遷移,然后查看數(shù)據(jù)庫表

三、關(guān)閉數(shù)值主鍵的標(biāo)識
數(shù)值主鍵的標(biāo)識DatabaseGeneratedOption是一個枚舉值,該枚舉值具有下面三個值:
DatabaseGeneratedOption.None:關(guān)閉數(shù)值主鍵。
DatabaseGeneratedOption.Identity:設(shè)置數(shù)值主鍵 自動增長 ,
DatabaseGeneratedOption.Computed :數(shù)值主鍵的值由計(jì)算得到(此列將無法插入值)。
1、設(shè)置關(guān)閉數(shù)值主鍵
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作為復(fù)合主鍵
this.HasKey(p => new {p .DepartmentId,p.Name});
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值不由數(shù)據(jù)庫生成。
this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
}2、插入數(shù)據(jù)庫表的時候DepartmentId列要顯示的指定值:
INSERT INTO Departments VALUES (1, '人事部',12.3,GETDATE());
四、指定屬性的最大長度
HasMaxLength可以設(shè)置表中列的最大長度。
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作為復(fù)合主鍵
this.HasKey(p => new {p .DepartmentId,p.Name});
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值不由數(shù)據(jù)庫生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值由數(shù)據(jù)庫自動生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Name屬性不應(yīng)超過 50 個字符。如果其值超過 50 個字符,則出現(xiàn) DbEntityValidationException 異常。
//如果 Code First 基于此模型創(chuàng)建數(shù)據(jù)庫,它還會將 Name 列的最大長度設(shè)置為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<Department>
{
public DepartmentMap()
{
// 使用匿名類的方式配置DepartmentId和Name作為復(fù)合主鍵
this.HasKey(p => new {p .DepartmentId,p.Name});
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值不由數(shù)據(jù)庫生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值由數(shù)據(jù)庫自動生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Name屬性不應(yīng)超過 50 個字符。如果其值超過 50 個字符,則出現(xiàn) DbEntityValidationException 異常。
//如果 Code First 基于此模型創(chuàng)建數(shù)據(jù)庫,它還會將 Name 列的最大長度設(shè)置為50 個字符。
this.Property(p => p.Name).HasMaxLength(50);
/*
Name屬性是必需的。如果不指定 Name,則出現(xiàn) DbEntityValidationException 異常。如果 Code First 基于此模型創(chuàng)建數(shù)據(jù)庫,則用于存儲此屬性的列將不可為空。
*/
this.Property(p => p.Name).IsRequired();
}
}
}六、指定不將CLR 屬性映射到數(shù)據(jù)庫中的列
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作為復(fù)合主鍵
this.HasKey(p => new {p .DepartmentId,p.Name});
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值不由數(shù)據(jù)庫生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
// 以下示例將DepartmentID 屬性設(shè)置為System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示該值由數(shù)據(jù)庫自動生成。
//this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Name屬性不應(yīng)超過 50 個字符。如果其值超過 50 個字符,則出現(xiàn) DbEntityValidationException 異常。
//如果 Code First 基于此模型創(chuàng)建數(shù)據(jù)庫,它還會將 Name 列的最大長度設(shè)置為50 個字符。
this.Property(p => p.Name).HasMaxLength(50);
/*
Name屬性是必需的。如果不指定 Name,則出現(xiàn) DbEntityValidationException 異常。如果 Code First 基于此模型創(chuàng)建數(shù)據(jù)庫,則用于存儲此屬性的列將不可為空。
*/
this.Property(p => p.Name).IsRequired();
/*
以下示例顯示如何指定CLR 類型的屬性不映射到數(shù)據(jù)庫中的列。
Ignore 等同于數(shù)據(jù)注解NotMapped
*/
this.Ignore(p => p.Budget);
}
}
}七、將CLR 屬性映射到數(shù)據(jù)庫中的特定列
HasColumnName可以用來設(shè)置映射到數(shù)據(jù)庫表中列的列名。
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 數(shù)據(jù)庫列。
*/
this.Property(p => p.Price).HasColumnName("ProductPrice");
}
}
}八、配置字符串屬性是否支持Unicode 內(nèi)容
IsUnicode()方法可以用來設(shè)置是否支持Unicode字符,該方法有兩個重載函數(shù)。
1、沒有參數(shù)的重載,默認(rèn)支持Unicode字符

2、有參數(shù)的重載,參數(shù)為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 數(shù)據(jù)庫列。
*/
this.Property(p => p.Price).HasColumnName("ProductPrice");
/*
* 默認(rèn)情況下,字符串為Unicode(SQLServer 中的nvarchar)。您可以使用IsUnicode 方法指定字符串應(yīng)為varchar 類型。
*/
this.Property(p => p.PlaceOfOrigin).IsUnicode(false);
}
}
}查看數(shù)據(jù)庫列類型:

九、配置數(shù)據(jù)庫列的數(shù)據(jù)類型
HasColumnType 方法支持映射到相同基本類型的不同表示。
/*
HasColumnType 方法支持映射到相同基本類型的不同表示。使用此方法并不支持在運(yùn)行時執(zhí)行任何數(shù)據(jù)轉(zhuǎn)換。
* 請注意,IsUnicode 是將列設(shè)置為 varchar 的首選方法,因?yàn)樗c數(shù)據(jù)庫無關(guān)。
*/
this.Property(p => p.Name).HasColumnType("varchar");十、配置復(fù)雜類型的屬性
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()
{
/*可以使用點(diǎn)表示法訪問復(fù)雜類型的屬性。
設(shè)置Course類里面的Department屬性的Name的最大長度是32
*/
this.Property(p => p.Department.Name).HasMaxLength(32);
}
}
}十一、將CLR 實(shí)體類型映射到數(shù)據(jù)庫中的特定表
/*Department 的所有屬性都將映射到名為 t_ Department 的表中的列。*/
ToTable("t_Department");
/*您也可以這樣指定架構(gòu)名稱:*/
ToTable("t_Department", "school");代碼地址:點(diǎn)此下載
到此這篇關(guān)于Entity Framework使用Fluent API配置案例的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Entity?Framework實(shí)現(xiàn)數(shù)據(jù)遷移
- Entity?Framework使用配置伙伴創(chuàng)建數(shù)據(jù)庫
- Entity Framework使用DbModelBuilder API創(chuàng)建表結(jié)構(gòu)
- Entity Framework常用查詢語句
- Entity Framework中執(zhí)行sql語句
- Entity Framework系統(tǒng)架構(gòu)與原理介紹
- Entity?Framework?Core實(shí)現(xiàn)Like查詢詳解
- Entity Framework Core批處理SQL語句
- Entity Framework Core實(shí)現(xiàn)軟刪除與查詢過濾器
- Entity Framework Core生成列并跟蹤列記錄
- Entity?Framework實(shí)體拆分多個表
相關(guān)文章
ASP.NET2.0 SQL Server數(shù)據(jù)庫連接詳解
本文將詳細(xì)介紹如何使用Connection對象連接數(shù)據(jù)庫 。對于不同的.NET 數(shù)據(jù)提供者,ADO.NET采用不同的Connection對象連接數(shù)據(jù)庫。這些Connection對象為我們屏蔽了具體的實(shí)現(xiàn)細(xì)節(jié),并提供了一種統(tǒng)一的實(shí)現(xiàn)方法。2009-07-07
asp.net Urlrewriter在虛擬主機(jī)上的使用方法
在網(wǎng)上看到,很多朋友在asp.net中做urlrewrite,用的是HttpHandle Server.Transfer的方法。其實(shí)這種方法是錯誤的。2009-12-12
.Net Core 實(shí)現(xiàn)圖片驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹了.Net Core 實(shí)現(xiàn)圖片驗(yàn)證碼的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
在dropDownList中實(shí)現(xiàn)既能輸入一個新值又能實(shí)現(xiàn)下拉選的代碼
在dropDownList中實(shí)現(xiàn)既能輸入一個新值,又能實(shí)現(xiàn)下拉選項(xiàng),想必很多的朋友已經(jīng)為此功能按耐不住了吧,接下來與大家分享下如何實(shí)現(xiàn),感興趣的朋友可以參考下哈2013-04-04
asp.net 實(shí)現(xiàn)靜態(tài)頁面累加訪問量的三種方式
asp.net 實(shí)現(xiàn)靜態(tài)頁面累加訪問量的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-03-03
Asp.NET MVC中使用SignalR實(shí)現(xiàn)推送功能
這篇文章主要為大家詳細(xì)介紹了Asp.NET MVC 中使用 SignalR 實(shí)現(xiàn)推送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Asp.net 中mvc 實(shí)現(xiàn)超時彈窗后跳轉(zhuǎn)功能
這篇文章主要介紹了Asp.net 中mvc 實(shí)現(xiàn)超時彈窗后跳轉(zhuǎn)功能,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
.NET Core 1.0創(chuàng)建Self-Contained控制臺應(yīng)用
這篇文章主要為大家詳細(xì)介紹了.NET Core 1.0創(chuàng)建Self-Contained控制臺應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
詳解ASP.NET MVC 利用Razor引擎生成靜態(tài)頁
本篇文章主要介紹了ASP.NET MVC 利用Razor引擎生成靜態(tài)頁,詳細(xì)的介紹了原理和步驟,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03

