[英]Mapping entity framework model to multiple tables
如何将实体框架模型映射到多个表? 如何对特定表执行插入操作(通过引用存储表名的字符串)?
我还没有实现这一点,但快速搜索提供了许多很好的例子,说明了一种称为Entity Splitting的实践。 以下应该是有用的:
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 能够根据其属性创建不同的模型。
你必须使用实体拆分
这是一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.