Asp.Net EF配置方式FluentAPI

基本EF配置只需要配置实体类和表、字段的对应关系、表间关联关系即可。如果利用EF的高级配置,可以达到更多效果;如果数据错误(比如字段不能为空、字符串超长等),会在EF层就报错,而不会提交到数据库服务器再报错。

1.HasMaxLength设定字段得最大长度

file

发生数据错误时会在EF层拦截,抛出DbEntityValidtionException异常,下图方式可以查看异常的错误信息:

catch(DbEntityValidationException valEx)
{
         foreach(DbEntityValidationResult err in vslEx.EntityValidationErrors)
            {
             foreach(DbValidationError in err.ValidationErrors)
                 {
                Console.WriteLine("错误:"+ve.PropertyName+","+ve.ErrorMessage);
                 }
             }    
}

2.字段是否为空

IsRequired属性不能为空;

IsOptional属性可以为空;

EF默认规则:主键属性不允许为空,引用类型允许为空,值类型不允许为空。

3.其它配置

a.主键:HasKey(p=>p.PId)

b.某个字段不参与映射数据库:Ignore(p=>p.Name)

c.是否对应固定长度类型:Property(p=>p.Name).IsFixedLength();varchar 和 char的区别

d.Property(p=>p.Name).IsUnicode(false);对应数据库类型是varchar类型,而不是nvarchar。nvarchar是Unicode类型

e.Property(p=>p.Name).HasColumnName("Name1");属性Name对应数据库字段Name1

f.Property(p=>p.Id).HasDatabaseGeneratedOption()

4.一对多关系映射

基本方法:this.Has***(p=>p.A).With***(),当前这个表和A属性的表的关系是Has定义,With定义的是A对应的表和这个表的关系。optional/required/many,可选的/必须的/多很多,

0...1/1/1* 。

5.双向关系的设计

public virtual ICollection<Student> Students {get;set;} = new List<Student>();属性

6.一个类中有多个外键属性

this.HasRequired(p=>p.Class).WithMany(p=>p.Students).HasForeignKey(p=>p.ClassId);

当类中的外键属性与数据库表中的外键字段名称不一致时,可以到类配置(***Config)中配置相关的条件。

7.多对多关系

this.HasMany(p=>p.Students).WithMany().Map(p=>p.ToTable("T_TeacherStudentRelations").MapLeftKey("TeacherId").MapRightKey("StudentId"));

file

总结: 一对多的配置

1、多端

public class Student

{

public long Id{get; set; } 

public string Name{get; set; }

 public long Classld{get; set;} 

public virtual Class Class{get; set; }

}

2、一端

public class Class

{

public long ld{get: set;}

public string Name {get; set;}

}

3、多端的配置(StudentConfig)中

this.HasRequired(p=>p.Class).WithMany().HasForeignKey(p=>p.ClassId)

一对多的配置(在一端配置ー个集合性,极端不推荐)

1、多端

public class Student

{

public long Id{get; set; } 

public string Name{get; set; }

 public long Classld{get; set;} 

public virtual Class Class{get; set; }

}

2、一端

public class Class

{

public long ld{get: set;}

public string Name {get; set;}

 public virtual Icollection<Student> Students{get; set; } =new List<Student>();

}

3、多端的配置(StudentConfig)中

this.HasRequired(p=>p.Class).WithMany(p=>p.Students).HasForeignKey(p=>p.ClassId)//WidthMany参数不能丢

多对多的配置

1、两端模型

public class Student

{

public long ld{get: set;}

public string Name {get; set;}

 public virtual Icollection<Teacher> Students{get; set; } =new List<Teacher>();

}

public class Teacher

{

public long ld{get: set;}

public string Name {get; set;}

 public virtual Icollection<Student> Students{get; set; } =new List<Student>();

}

2、在其中一端配置(StudentConfig)

this.HasRequired(p=>p.Teachers).WithMany(p=>p.Students).Map(p=>p.ToTable("T_StudentTeachersRelations").MapLeftKey("StudentId").MapRight("TeacherId"));//不要忘记WithMany的参数

本文由博客群发一文多发等运营工具平台 OpenWrite 发布

猜你喜欢

转载自blog.csdn.net/weixin_39630549/article/details/115129981