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

现在,我们来学习怎么使用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方法中。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值