简体   繁体   English

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

[英]Mapping entity framework model to multiple tables

How can I map an entity framework model to multiple tables?如何将实体框架模型映射到多个表? How to perform insertion operation to specific table (by reference of string which stores the table name)?如何对特定表执行插入操作(通过引用存储表名的字符串)?

I have not implemented this but a quick search provides many good examples of a practice known as Entity Splitting .我还没有实现这一点,但快速搜索提供了许多很好的例子,说明了一种称为Entity Splitting的实践。 The following should be useful:以下应该是有用的:

http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-splitting-in-entity-framework-6-code-first-approach/ 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");  
      });  
   }  
}

All credit for the above code goes to linked post以上代码的所有功劳都归于链接帖子

In this case you can use IModelCacheKeyFactory , which allow to hook into the model caching mechanism so EF is able to create different models based on its property.在这种情况下,您可以使用IModelCacheKeyFactory ,它允许挂钩模型缓存机制,以便 EF 能够根据其属性创建不同的模型。

This article explains how 这篇文章解释了如何

you must use Entity splitting 你必须使用实体拆分

here is an example for it. 是一个例子。

I am sharing with you a tutorial link. 我正在与您分享教程链接。 you can understand the concept of Entity splitting thoroughly. 你可以彻底理解实体分裂的概念。 In this tutorial, tutor explains the Entity splitting by a scenario in which he has two tables for user info. 在本教程中,教师通过一个场景解释实体拆分,其中他有两个用户信息表。 in one table he has user name etc and in other table he has user's address. 在一个表中他有用户名等,而在另一个表中他有用户的地址。 but at application level, there is no separation between user details and user's address. 但在应用程序级别,用户详细信息和用户地址之间没有分离。 and when user saves data, it goes to two different tables. 当用户保存数据时,它会转到两个不同的表。

here is tutorial link. 是教程链接。

you can also download it from torrent for free. 你也可以免费下载torrent。

torrent link 种子链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM