[英]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.