将实体框架模型映射到多个表

Sas*_*asi 6 .net c# asp.net-mvc entity-framework

如何将实体框架模型映射到多个表?如何对特定表执行插入操作(通过引用存储表名的字符串)?

Ant*_*h12 6

我还没有实现这一点,但快速搜索提供了许多很好的例子,说明了一种称为Entity Splitting的实践。以下应该是有用的:

http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-splitting-in-entity-framework-6-code-first-approach/

public partial class Employee  
{  
   // These fields come from the “Employee” table  
   public int EmployeeId { get; set; }   
   public string Code { get; set; }  
   public string Name { get; set; }  

   // These fields come from the “EmployeeDetails” table  
   public string PhoneNumber { get; set; }  
   public string EmailAddress { get; set; }  
} 

public partial class Model : DbContext  
{  
   public Model() : base("name=EntityModel")  
   {  
      Database.Log = Console.WriteLine;  
   }  
   public virtual DbSet<Employee> Employees { get; set; }  

   protected override void OnModelCreating(DbModelBuilder modelBuilder)  
   {  
      modelBuilder.Entity<Employee>()  
      .Map(map =>  
      {  
          map.Properties(p => new  
          {  
             p.EmployeeId,  
             p.Name,  
             p.Code  
          });  
          map.ToTable("Employee");  
      })  
      // Map to the Users table  
      .Map(map =>  
      {  
          map.Properties(p => new  
          {  
             p.PhoneNumber,  
             p.EmailAddress  
          });  
          map.ToTable("EmployeeDetails");  
      });  
   }  
}
Run Code Online (Sandbox Code Playgroud)

以上代码的所有功劳都归于链接帖子