gpt4 book ai didi

c# - EF4.4 中每个具体类型 (TPC) 映射的表

转载 作者:行者123 更新时间:2023-11-30 16:20:12 25 4
gpt4 key购买 nike

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

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

谢谢!

最佳答案

像这样改变你的配置:

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();
}

关于c# - EF4.4 中每个具体类型 (TPC) 映射的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14525591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com