约定配置
1、主要规则
-
表名采用DbContext
中对应的DbSet
的属性名
-
数据表列的名字采用实体类属性的名字, 列的数据类型采用喝实体类属性类型最兼容的类型, 可以自定义设置
-
数据表列的可空性取决于对应实体类属性的可空性
-
名字为Id
的属性为主键
-
如果主键为short
, int
或者long
则默认采用自增
-
如果主键为Guid
, 则默认采用Guid
生成机制生成主键值
2、两种配置方法
(0)两种方法结合
(1)Data Annotation
| [Table("T_Books")] |
| public class Book |
| { |
| public long Id { get; set; } |
| [Required] |
| [MaxLength(50)] |
| public string Title { get; set; } |
| public DateTime PubTime { get; set; } |
| public double Price { get; set; } |
| [Required] |
| [MaxLength(50)] |
| public string AuthoName { get; set; } |
| } |
(2)Fluent API
单独写个Configuration
, 常用
| |
| public class BookConfig : IEntityTypeConfiguration<Book> |
| { |
| public void Configure(EntityTypeBuilder<Book> builder) |
| { |
| |
| builder.ToTable("T_Books"); |
| |
| builder.Property(b => b.Title).HasMaxLength(50).IsRequired(); |
| builder.Property(b => b.AuthoName).HasMaxLength(20).IsRequired(); |
| } |
| } |
| |
| public class MyDbContext : DbContext |
| { |
| |
| public DbSet<Book> Books { get; set; } |
| |
| |
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| { |
| base.OnConfiguring(optionsBuilder); |
| |
| optionsBuilder.UseSqlServer("Server =.; Database = demo1; Trusted Connection = True; MultipleActiveResultSets = true"); |
| } |
| |
| |
| protected override void OnModelCreating(ModelBuilder modelBuilder) |
| { |
| base.OnModelCreating(modelBuilder); |
| |
| modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); |
| } |
| } |
Fluent API
1、API
(1)视图
| modelBuilder.Entity<Blog>().ToVie("blogsView"); |
(2)排除属性映射
| public class BookConfig : IEntityTypeConfiguration<Book> |
| { |
| public void Configure(EntityTypeBuilder<Book> builder) |
| { |
| |
| builder.ToTable("T_Books"); |
| |
| builder.Ignore(b => b.Price); |
| } |
| } |
(3)设置列名
| public class BookConfig : IEntityTypeConfiguration<Book> |
| { |
| public void Configure(EntityTypeBuilder<Book> builder) |
| { |
| |
| builder.ToTable("T_Books"); |
| |
| builder.Property(b => b.PubTime).HasColumnName("pub_time"); |
| } |
| } |
| modelBuilder.Entity<T>(e => e.Prop).HasColumnName("prop"); |
(4)设置列数据类型
| public class BookConfig : IEntityTypeConfiguration<Book> |
| { |
| public void Configure(EntityTypeBuilder<Book> builder) |
| { |
| |
| builder.ToTable("T_Books"); |
| |
| builder.Property(b => b.Title).HasColumnType("varchar(200)"); |
| } |
| } |
| modelBuilder.Entity<T>(e => e.Prop).HasColumnName("prop"); |
(5)配置主键
| public class BookConfig : IEntityTypeConfiguration<Book> |
| { |
| public void Configure(EntityTypeBuilder<Book> builder) |
| { |
| |
| builder.ToTable("T_Books"); |
| |
| builder.HasKey(b => b.Id); |
| } |
| } |
| public class MyDbContext : DbContext |
| { |
| public DbSet<Book> Books { get; set; } |
| |
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| { |
| |
| } |
| |
| protected override void OnModelCreating(ModelBuilder modelBuilder) |
| { |
| base.OnModelCreating(modelBuilder); |
| |
| modelBuilder.Entity<Book>().HasKey(x => x.Id); |
| } |
| } |
(6)生成列的值
| builder.Property(b => b.Price).ValueGeneratedOnAdd(); |
(7)为属性设置默认值
| builder.Property(b => b.Title).HasDefaultValue("aaa"); |
| modelBuilder.Entity<Book>().Property(b => b.Title).HasDefaultValue("aaa"); |
(8)索引
A.索引
| builder.HasIndex(b => b.Title); |
| modelBuilder.Entity<Book>().HasIndex(b => b.Title); |
B.复合索引
| builder.HasIndex(b => new { b.Id, b.Title }); |
| modelBuilder.Entity<Book>().HasIndex(b => new { b.Id, b.Title }); |
C.唯一索引
| builder.HasIndex(b => b.Title).IsUnique(); |
| modelBuilder.Entity<Book>().HasIndex(b => b.Title).IsUnique(); |
D.聚合索引
| builder.HasIndex(b => b.Title).IsClustered(); |
| modelBuilder.Entity<Book>().HasIndex(b => b.Title).IsClustered(); |
2、尽量不要使用链式调用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律