EF Core 有两种实体配置方式
1、Data Annotation
优点:简单;缺点:耦合
[Table("Persons")] public class Person { public int Id{get;set;} [Required] [MaxLength(50)] public string Name{get;set;} }
2、Fluent API (推荐)
优点:解耦;缺点:较复杂
//实体类 public class Person { public int Id{get;set;} public string Name{get;set;} } // 实体配置类 pubic class PersonEntityTypeConfiguration:IEntityTypeConfiguration<Person> { public void Configure(EntityTypeBuilder<Person> builder) { //可以直接在OnModelCreating方法中 builder.Entity<Person>().ToTable("Persons") builder.ToTable("Persons"); builder.Property(t=>t.Name).HasMaxLength(50).IsRequired();//给Name设置最大长度,并且不为空 } }标签:实体,Name,get,builder,配置,Persons,set,EFCore,public From: https://www.cnblogs.com/lixiang1998/p/17798304.html