GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
 【 如果你想靠AI翻身,你先需要一个靠谱的工具! 】
要显式将某个属性设置为主键,可使用 HasKey 方法。在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Model{    publicclassProduct    {        publicintProductId { get; set; }        publicstringProductName { get; set; }        publicdecimalPrice { get; set; }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.Entity;usingSystem.Data.Entity.ModelConfiguration;usingFluentAPI.Model;namespaceFluentAPI.Data.FluentAPIMap{    publicclassProductMap : EntityTypeConfiguration<Product>    {        publicProductMap()        {            //使用 HasKey 方法对 Product 类型配置 ProductId 主键。            this.HasKey(p => p.ProductId);        }    }} | 

以下示例配置要作为Department 类型的组合主键的DepartmentID 和 Name 属性。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Model{    publicclassDepartment    {        publicintDepartmentId { get; set; }        publicstringName { get; set; }        publicdecimalBudget { get; set; }        publicDateTime StartDate { get; set; }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassDepartmentMap : EntityTypeConfiguration<Department>    {        publicDepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new{p .DepartmentId,p.Name});        }    }} | 
使用EF的数据迁移,然后查看数据库表

数值主键的标识DatabaseGeneratedOption是一个枚举值,该枚举值具有下面三个值:
DatabaseGeneratedOption.None:关闭数值主键。
DatabaseGeneratedOption.Identity:设置数值主键 自动增长 ,
DatabaseGeneratedOption.Computed :数值主键的值由计算得到(此列将无法插入值)。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassDepartmentMap : EntityTypeConfiguration<Department>    {        publicDepartmentMap()        {            // 使用匿名类的方式配置DepartmentId和Name作为复合主键            this.HasKey(p => new{p .DepartmentId,p.Name});            // 以下示例将DepartmentID 属性设置为System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None,以指示该值不由数据库生成。            this.Property(p => p.DepartmentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);        }    }} | 
| 1 | INSERTINTODepartments VALUES(1, '人事部',12.3,GETDATE()); | 
HasMaxLength可以设置表中列的最大长度。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassDepartmentMap : EntityTypeConfiguration<Department>    {        publicDepartmentMap()        {            // 使用匿名类的方式配置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);        }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassDepartmentMap : EntityTypeConfiguration<Department>    {        publicDepartmentMap()        {            // 使用匿名类的方式配置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();        }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassDepartmentMap : EntityTypeConfiguration<Department>    {        publicDepartmentMap()        {            // 使用匿名类的方式配置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可以用来设置映射到数据库表中列的列名。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.Entity;usingSystem.Data.Entity.ModelConfiguration;usingFluentAPI.Model;usingSystem.ComponentModel.DataAnnotations.Schema;namespaceFluentAPI.Data.FluentAPIMap{    publicclassProductMap : EntityTypeConfiguration<Product>    {        publicProductMap()        {            //使用 HasKey 方法对 Product 类型配置 ProductId 主键。            this.HasKey(p => p.ProductId);            /*             * 以下示例将Price CLR 属性映射到ProductPrice 数据库列。             */            this.Property(p => p.Price).HasColumnName("ProductPrice");        }    }} | 
IsUnicode()方法可以用来设置是否支持Unicode字符,该方法有两个重载函数。


| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.Entity;usingSystem.Data.Entity.ModelConfiguration;usingFluentAPI.Model;usingSystem.ComponentModel.DataAnnotations.Schema;namespaceFluentAPI.Data.FluentAPIMap{    publicclassProductMap : EntityTypeConfiguration<Product>    {        publicProductMap()        {            //使用 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 方法支持映射到相同基本类型的不同表示。
| 1 2 3 4 5 | /*HasColumnType 方法支持映射到相同基本类型的不同表示。使用此方法并不支持在运行时执行任何数据转换。* 请注意,IsUnicode 是将列设置为 varchar 的首选方法,因为它与数据库无关。*/this.Property(p => p.Name).HasColumnType("varchar"); | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPIApp.Model{    publicclassCourse    {        publicintCourseID { get; set; }        publicstringTitle { get; set; }        publicintCredits { get; set; }        publicvirtualDepartment Department { get; set; }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | usingFluentAPI.Model;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceFluentAPI.Data.FluentAPIMap{    publicclassCourseMap : EntityTypeConfiguration<Course>    {        publicCourseMap()        {            /*可以使用点表示法访问复杂类型的属性。              设置Course类里面的Department属性的Name的最大长度是32             */            this.Property(p => p.Department.Name).HasMaxLength(32);        }    }} | 
| 1 2 3 4 | /*Department 的所有属性都将映射到名为 t_ Department 的表中的列。*/ToTable("t_Department");/*您也可以这样指定架构名称:*/ToTable("t_Department", "school"); | 
代码地址:点此下载
到此这篇关于Entity Framework使用Fluent API配置案例的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。