首页 > 移动平台 > 详细

Entity Framework Code-First(10.2):Entity Mappings

时间:2016-07-05 17:12:38      阅读:219      评论:0      收藏:0      [点我收藏+]

Entity Mappings using Fluent API:

Here, we will learn how to configure an entity using Fluent API.

We will use the following Student and Standard domain classes of the school application.

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Standard Standard { get; set; }
}
    
public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
   
 }

 

Configure Default Schema:

First, let‘s configure a default schema for the tables in the database. However, you can change the schema while creating the individual tables. The following example sets the default Admin schema.

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:

Code-First will create the database tables with the name of DbSet properties in the context class - Students and Standards in this case. You can override this convention and can give a different table name than the DbSet properties, as shown below.

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");

        }
    }
}

 

As you can see in the above example, we start with the Entity<TEntity>() method. Most of the time, you have to start with the Entity<TEntity>() method to configure it using Fluent API. We have used ToTable() method to map Student entity to StudentInfo and Standard entity to StandardInfo table. Notice that StudentInfo is in Admin schema and StandardInfo table is in dbo schema because we have specified dbo schema for StandardInfo table.

技术分享

Map Entity to Multiple Table:

The following example shows how to map Student entity to multiple tables in the database.

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");

        }
    }
}

 

As you can see in the above example, we mapped some properties of Student entity to StudentInfo table and other properties to StudentInfoDetail table using Map() method. Thus, Student entity will split into two tables, as shown below.

技术分享

Map method need the delegate method as a parameter. You can pass Action delegate or lambda expression in Map method, as shown below.

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");

        }
    }
}

 

Entity Framework Code-First(10.2):Entity Mappings

原文:http://www.cnblogs.com/purplefox2008/p/5644246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!