我尝试对所有标准字段使用基本模型,而任何其他模型(例如LocationIssueModel和PersonIssueModel)将具有不同的属性。表结构是相同的,其中location_issue将具有所需的字段,其中person_issue将具有它需要的所有字段,而主要问题表用于公共字段。 location_issue和person_issue都有一个PK,它是问题表上的issue_ky的FK。
使用以下配置并尝试执行Linq查询并在IssueModel1上抛出错误。
此配置是否正确?
编辑:由于使用IsActiveLocation标志的接口需要使用实体拆分
模型
public class IssueModel
{
public int Key { get; set; }
public string RequestNumber { get; set; }
public bool IsActiveLocation { get; set; }
}
public class LocationIssueModel : IssueModel
{
public string LocationName { get; set; }
}
public class PersonIssueModel : IssueModel
{
public string PersonName { get; set; }
}
配置
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<LocationIssueModel>()
.Map(m =>
{
m.Properties(t => new { t.Key, t.RequestNumber });
m.ToTable("issue");
})
.Map(m =>
{
m.Properties(t => new { t.Key, t.LocationName });
m.ToTable("location_issue");
});
}
SQL
SELECT '0X0X' AS [C1],
[Extent1].[issue_ky] AS [issue_ky],
[Extent3].[request_nbr] AS [request_nbr],
[Extent1].[location_ky] AS [location_ky],
FROM [dbo].[Issue1] AS [Extent1]
INNER JOIN [wm].[location_issue] AS [Extent2] ON [Extent1].[issue_ky] = [Extent2].[issue_ky]
INNER JOIN [wm].[issue] AS [Extent3] ON [Extent1].[issue_ky] = [Extent3].[issue_ky]
正如您所看到的,生成的sql不是预期的。
答案 0 :(得分:1)
您的配置错误,如果我理解正确您想要执行每个类型的表(TPT)继承,可以这样做:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<LocationIssueModel>().ToTable("LocationIssues");
modelBuilder.Entity<PersonIssueModel>().ToTable("PersonIssues");
}
查看此链接以了解有关此类继承的更多信息:http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt