3

我正在尝试在 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"));
    }
}

关于如何正确配置的任何想法?

谢谢!

4

1 回答 1

3

像这样更改您的配置:

public class MainHierarchyLevelConfiguration : EntityTypeConfiguration<MainHierarchyLevel>
{
    public MainHierarchyLevelConfiguration()
    {
        Map(mb =>
        {
            mb.MapInheritedProperties();
            mb.ToTable("MainHierarchyLevels");
        });
    }
}

public class MainHierarchyItemsConfiguration : EntityTypeConfiguration<MainHierarchyItem>
{
    public MainHierarchyItemsConfiguration()
    {
        Map(mb =>
        {
            mb.MapInheritedProperties();
            mb.ToTable("MainHierarchyItems");
        });
        HasKey(hierarchyItem => hierarchyItem.Id);
        HasRequired(hierarchyItem => hierarchyItem.Level).WithMany().Map(e => e.MapKey("Level_Id"));
    }
}

public class HierarchyContext : DbContext
{
    public DbSet<MainHierarchyLevel> MainHierarchyLevels { get; set; }
    public DbSet<MainHierarchyItem> MainHierarchyItems { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new MainHierarchyLevelConfiguration());
        modelBuilder.Configurations.Add(new MainHierarchyItemsConfiguration());
    }
}

运行以下测试程序时,将创建 2 个表 - 一个用于两个具体类 MainHierarchyLevel 和 MainHierarchyItem 中的每一个。

using (var db = new HierarchyContext())
{

    var level = new MainHierarchyLevel { Name = "Level1" };
    level = db.MainHierarchyLevels.Add(level);

    var mh = new MainHierarchyItem { Name = "m1", Description = "m1 desc", Level = level };
    db.MainHierarchyItems.Add(mh);
    db.SaveChanges();

    var query = from m in db.MainHierarchyItems
                orderby m.Name
                select m;

    Console.WriteLine("All mains in the database:");
    foreach (var item in query)
    {
        Console.WriteLine(item.Name);
    }

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}
于 2013-02-02T10:33:52.643 回答