I’m trying to achieve a TPC design on EF 4.4.
I have a set of classes that are already mapped to existing tables, and adding a new set with the same structure, that are to be mapped to different tables, with disjoint IDs.
So here’s pretty much the new design for the old classes (without the new type of hierarchy that I’m going to add).
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 { }
No matter what I do with the configuration, EF always makes a table name up, e.g. I get a query like
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]
Where HierarchyItems1 is made up by EF.
Here’s an example of what i’m doing with the configuration:
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"));
}
}
Any ideas on how to configure this correctly?
Thank you!
Change your config like this:
When running the following test program, 2 tables will be created – one for each of the two concrete classes MainHierarchyLevel and MainHierarchyItem.