本篇内容介绍了“Entity Framework使用Fluent api配置的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
本篇内容介绍了“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); 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); this.Property(p => p.Name).IsRequired(); 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); 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); this.Property(p => p.Price).HasColumnName("ProductPrice"); this.Property(p => p.PlaceOfOrigin).IsUnicode(false); } }}
查看数据库列类型:
HasColumnType 方法支持映射到相同基本类型的不同表示。
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() { this.Property(p => p.Department.Name).HasMaxLength(32); } }}
ToTable("t_Department");ToTable("t_Department", "school");
“Entity Framework使用Fluent API配置的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
--结束END--
本文标题: Entity Framework使用Fluent API配置的方法
本文链接: https://www.lsjlt.com/news/323969.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
2024-05-05
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0