Code First 是 Entity Framework Core (简称 EF Core) 的一种开发模式,它允许开发人员使用纯粹的代码来定义数据模型,通过它,可以极大地提高开发效率:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
public class Article { /// <summary> /// 主键 /// </summary> public long Id { get; set; } /// <summary> /// 标题 /// </summary> public string Title { get; set; } /// <summary> /// 内容 /// </summary> public string Content { get; set; } /// <summary> /// 此文章的若干条评论 /// </summary> public List<Comment> Comments { get; set; } = new List<Comment>(); } public class Comment { public long Id { get; set; } public Article Article { get; set; } public long ArticleId { get; set; } public string Message { get; set; } }
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; // IEntityTypeConfiguration的泛型参数类指定这个类要对实体类 Article 进行配置 class ArticleConfig : IEntityTypeConfiguration<Article> { // 使用Fluent API的方式对实体类进行配置 // 也可以在实体类中使用 Data Annotation 进行配置,但那样耦合太深,不推荐使用 public void Configure(EntityTypeBuilder<Article> builder) { // 表示这个实体类对应数据库中名字为T_Articles的表 builder.ToTable("T_Articles"); builder.Property(p => p.Content).IsRequired().IsUnicode(); builder.Property(p => p.Title).IsRequired().IsUnicode() .HasMaxLength(255); } } class CommentConfig : IEntityTypeConfiguration<Comment> { public void Configure(EntityTypeBuilder<Comment> builder) { builder.ToTable("T_Comments"); // 一条评论对应一篇文章,一篇文章有多条评论 builder.HasOne<Article>(c =>c.Article) .WithMany(a => a.Comments) .IsRequired() .HasForeignKey(c => c.ArticleId); builder.Property(p=>p.Message).IsRequired().IsUnicode(); } }
using Microsoft.EntityFrameworkCore; class TestDbContext: DbContext { public DbSet<Article> Articles { get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connStr = "Server=(localdb)\\mssqllocaldb;Database=TestDB;Trusted_Connection=True;MultipleActiveResultSets=true"; optionsBuilder.UseSqlServer(connStr); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // 表示加载当前程序集中所有实现了IEntityTypeConfiguration接口的类 modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } }
Add-Migration InitialCreate 【InitialCreate 名字可随意取,有意义就好】
说明:Add-Migration命令会自动在项目的Migrations文件夹中生成C#代码
Update-database
说明:编译并且执行数据库迁移代码
modelBuilder.Entity<Blog>().ToView("blogsView");
modelBuilder.Entity<Blog>().Ignore(b => b. Name2);
modelBuilder.Entity<Blog>().Property(b =>b.BlogId).HasColumnName("blog_id");
builder.Property(e => e.Title) .HasColumnType("varchar(200)")
modelBuilder.Entity<Student>().HasKey(c => c.Number);
modelBuilder.Entity<Blog>().HasIndex(b => b.Url); // 复合索引 modelBuilder.Entity<Person>().HasIndex(p => new { p.FirstName, p.LastName });
builder.HasMany<Teacher>(c => c.Teachers).WithMany(t => t.Students) .UsingEntity(j => j.ToTable("T_Students_Teachers"));
builder.HasOne<Article>(c =>c.Article) .WithMany(a => a.Comments) .IsRequired() .HasForeignKey(c => c.ArticleId);
builder.HasOne<Delivery>(c => c.Delivery).WithOne(d => d.Order) .HasForeignKey<Delivery>(d => d.OrderId);