我正在尝试在 EF 4.4 上实现 TPC 设计。
我有一组已经映射到现有表的类,并添加了一个具有相同结构的新集合,这些类将映射到具有不相交 ID 的不同表。
所以这几乎是旧类的新设计(没有我要添加的新类型的层次结构)。
public abstract class HierarchyLevel
{
public virtual int Id { get; set; }
public string Name { get; set; }
}
public class MainHierarchyLevel : HierarchyLevel { }
public abstract class HierarchyItem
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual HierarchyLevel Level { get; set; }
}
public class MainHierarchyItem : HierarchyItem { }
public abstract class HierarchyTreeItem
{
public int Id { get; set; }
public virtual HierarchyTreeItem ParentTreeItem { get; set; }
public virtual HierarchyItem Parent { get; set; }
public virtual HierarchyItem Child { get; set; }
}
public class MainHierarchyTreeItem : HierarchyTreeItem { }
无论我对配置做什么,EF 总是会创建一个表名,例如,我得到一个类似的查询
SELECT
[Extent1].[Id] AS [Id],
'0X0X' AS [C1],
[Extent2].[Name] AS [Name],
[Extent2].[Description] AS [Description],
[Extent2].[Level_Id] AS [Level_Id]
FROM [dbo].[HierarchyItems] AS [Extent1]
INNER JOIN [dbo].[HierarchyItems1] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
HierarchyItems1 由 EF 组成。
这是我对配置所做的示例:
public class HierarchyItemsConfiguration : EntityTypeConfiguration<HierarchyItem>
{
public HierarchyItemsConfiguration()
{
Property(hierarchyItem => hierarchyItem.Id).HasColumnName("Id").IsRequired();
Property(hierarchyItem => hierarchyItem.Name).HasColumnName("Name").IsRequired();
Property(hierarchyItem => hierarchyItem.Description).HasColumnName("Description").IsOptional();
}
}
public class MainHierarchyItemsConfiguration : EntityTypeConfiguration<MainHierarchyItem>
{
public MainHierarchyItemsConfiguration()
{
Map(mb =>
{
mb.MapInheritedProperties();
mb.ToTable("HierarchyItems");
});
ToTable("HierarchyItems");
HasKey(hierarchyItem => hierarchyItem.Id);
HasRequired(hierarchyItem => hierarchyItem.Level).WithMany().Map(e => e.MapKey("Level_Id"));
}
}
关于如何正确配置的任何想法?
谢谢!