码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-12-12 21:48:54      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/caofangsheng/p/5041805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!