现在,我们来学习怎么使用Fluent API来配置实体。
一。配置默认的数据表Schema
Student实体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF4 { public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public int StuaentAge { get; set; } public string StudentEmail { get; set; } public Standard Standard { get; set; } } }
Standard实体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF4 { public class Standard { public int StandardID { get; set; } public int StandardName { get; set; } public ICollection<Student> Students { get; set; } } }
上下文类:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF4 { public class DBContextClass:DbContext { public DBContextClass() : base("ConnectionStrings") { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("hello"); base.OnModelCreating(modelBuilder); } } }
然后生成的数据表Schema就是我们配置的【hello】了
当然,你也可以分别对每个表,进行设置Schema。
二。把实体映射成表
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF4 { public class DBContextClass:DbContext { public DBContextClass() : base("ConnectionStrings") { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //配置数据表的Schema //modelBuilder.HasDefaultSchema("hello"); //将实体映射成表 modelBuilder.Entity<Student>().ToTable("WahHaHa"); modelBuilder.Entity<Standard>().ToTable("StandardInfo","xxx"); base.OnModelCreating(modelBuilder); } } }
然后数据库是这样的:
打开表一个一个看:
三。将一个实体,拆分成多个表。
下面的代码是将Student实体拆分成两个表。
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF4 { public class DBContextClass:DbContext { public DBContextClass() : base("ConnectionStrings") { } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //配置数据表的Schema //modelBuilder.HasDefaultSchema("hello"); //将实体映射成表 //modelBuilder.Entity<Student>().ToTable("WahHaHa"); //modelBuilder.Entity<Standard>().ToTable("StandardInfo","xxx"); //将一个实体映射成多个表 modelBuilder.Entity<Student>().Map( m => { m.Properties(p => p.StudentID); m.Properties(p => p.StudentName); m.ToTable("StudentInfo"); }).Map( m => { m.Properties(p => p.StudentID); m.Properties(p => p.StuaentAge); m.Properties(p => p.StudentEmail); m.ToTable("StudentDetails"); }); modelBuilder.Entity<Standard>().ToTable("StandardInfo"); base.OnModelCreating(modelBuilder); } } }
生成额数据库是:
上面的代码中,我们将Student实体,拆分成了两个表,StudentInfo和StudentDetail。
Map method need the delegate method as a parameter. You can pass Action delegate or lambda expression in Map method, as shown below.
Map方法需要委托作为参数。你可以传递一个Action委托或者lambda表达式在这个Map方法中。