文章出處

現在,我們來學習怎么使用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方法中。

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()