【Code-First系列】 使用Fluent API进行实体映射

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; }

    }
}

上下文类:

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】了,数据库表编程hello.Students

第二种方法:

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方法中。

你可能感兴趣的:(【Code-First系列】 使用Fluent API进行实体映射)