本篇內容介紹了“Entity Framework使用Fluent API配置的方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
要顯式將某個屬性設置為主鍵,可使用 HasKey 方法。在以下示例中,使用了 HasKey 方法對 Product 類型配置 ProductId 主鍵。
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; }
}
}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);
}
}
}
以下示例配置要作為Department 類型的組合主鍵的DepartmentID 和 Name 屬性。
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; }
}
}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});
}
}
}使用EF的數據遷移,然后查看數據庫表

數值主鍵的標識DatabaseGeneratedOption是一個枚舉值,該枚舉值具有下面三個值:
DatabaseGeneratedOption.None:關閉數值主鍵。
DatabaseGeneratedOption.Identity:設置數值主鍵 自動增長 ,
DatabaseGeneratedOption.Computed :數值主鍵的值由計算得到(此列將無法插入值)。
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);
}
}
}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(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作為復合主鍵
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();
}
}
}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(50);
/*
Name屬性是必需的。如果不指定 Name,則出現 DbEntityValidationException 異常。如果 Code First 基于此模型創建數據庫,則用于存儲此屬性的列將不可為空。
*/
this.Property(p => p.Name).IsRequired();
/*
以下示例顯示如何指定CLR 類型的屬性不映射到數據庫中的列。
Ignore 等同于數據注解NotMapped
*/
this.Ignore(p => p.Budget);
}
}
}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");
}
}
}IsUnicode()方法可以用來設置是否支持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");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(32);
}
}
}/*Department 的所有屬性都將映射到名為 t_ Department 的表中的列。*/
ToTable("t_Department");
/*您也可以這樣指定架構名稱:*/
ToTable("t_Department", "school");“Entity Framework使用Fluent API配置的方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。