10.1.翻譯系列:EF 6中的實體映射【EF 6 Code-First系列】

原文連接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.aspxhtml

EF 6 Code-First系列文章目錄:typescript

Fluent API能夠配置實體,爲其映射爲數據表,默認的模式等。數據庫

配置默認的模式

首先,讓咱們來配置數據庫中數據表的默認的模式名吧。固然你能夠在配置單獨的表的時候,改變這個默認的模式。下面的代碼設置默認的模式名爲Admin,全部的數據庫對象都將會是你配置的這個模式名。api

public class SchoolContext: DbContext 
{
    public SchoolDBContext(): base() {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        //Configure default schema
        modelBuilder.HasDefaultSchema("Admin");
    }
}
配置實體-->數據表

Code-First將會以上下文類中的DbSet類型的屬性,建立數據表。在這個例子中是Students和Standards表。你能夠單獨指定表名,以重寫DBset類型的屬性所生成的表,例如:app

namespace CodeFirst_FluentAPI_Tutorials
{
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            //Configure default schema
            modelBuilder.HasDefaultSchema("Admin");
                    
            //Map entity to table
            modelBuilder.Entity<Student>().ToTable("StudentInfo");
            modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo");
        }
    }
}

正如上面代碼所見,咱們配置以Entity()方法開始,大多數的時候,使用Fluent API,你必需要以這個方法開始。咱們使用ToTable()方法,將Student實體映射爲StudentInfo表,將Standard實體映射爲StandardInfo表,而且單獨配置了StandardInfo表的模式名爲dbo.
enter description hereide

映射實體爲多個表

下面的例子,演示了將Student實體,映射爲多個表。測試

namespace CodeFirst_FluentAPI_Tutorials
{
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student>().Map(m =>
            {
                m.Properties(p => new { p.StudentId, p.StudentName});
                m.ToTable("StudentInfo");
            }).Map(m => {
                m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth});
                m.ToTable("StudentInfoDetail");
            });

            modelBuilder.Entity<Standard>().ToTable("StandardInfo");
        }
    }
}

正如上面代碼所示,咱們將Student實體的一些屬性映射爲StudentInfo表,使用Map方法將Student實體的另一些屬性映射爲StudentInfoDetail表,因此Student實體將會拆分爲兩個數據表:
enter description hereui

Map方法須要一個委託類型的參數,你能夠在Man方法中傳遞Action委託或者lambda表達式,例如:spa

using System.Data.Entity.ModelConfiguration.Configuration;

namespace CodeFirst_FluentAPI_Tutorials
{
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Student>().Map(delegate(EntityMappingConfiguration<Student> studentConfig)
            {
                studentConfig.Properties(p => new { p.StudentId, p.StudentName });
                studentConfig.ToTable("StudentInfo");
            });

            Action<EntityMappingConfiguration<Student>> studentMapping = m =>
            {
                m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth });
                m.ToTable("StudentInfoDetail");
            };
            
            modelBuilder.Entity<Student>().Map(studentMapping);
            modelBuilder.Entity<Standard>().ToTable("StandardInfo");
        }
    }
}
相關文章
相關標籤/搜索