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

本文关键字:映射 实体 框架 模型 | 更新日期: 2023-09-27 17:57:06

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

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

我还没有实现这一点,但快速搜索提供了许多称为实体拆分的做法的好例子。以下内容应该有用:

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

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

在这种情况下,

您可以实现自己的IModelCacheKeyFactory,这允许挂钩到模型缓存机制,以便EF能够在运行时基于某些值创建不同的模型。

本文解释了如何