问题描述
我遇到的情况是,现有的数据库结构使用了适合TBH(逐层表)方案的鉴别器。例如,父母与子女的关系,其中歧视者同时适用于父母和子女。下面的示例概述了这种关系。
[Table("polyParents")]
public abstract class polyParent
{
[Key]
public int polyParentId { get; set; }
public string Name { get; set; }
public virtual ICollection<polyChild> Children { get; set; } = new List<polyChild>();
}
public class OneParent : polyParent
{
}
public class AnotherParent : polyParent
{
}
[Table("polyChildren")]
public abstract class polyChild
{
[Key]
public int polyChildId { get; set; }
public string Name { get; set; }
public virtual polyParent Parent { get; set; }
}
public class OneChild : polyChild
{
}
public class AnotherChild : polyChild
{
}
public class polyParentConfiguration : EntityTypeConfiguration<polyParent>
{
public polyParentConfiguration()
{
Map<TestParent>(x => x.Requires("polyType").HasValue(1));
Map<OtherParent>(x => x.Requires("polyType").HasValue(2));
}
}
public class polyChildConfiguration : EntityTypeConfiguration<polyChild>
{
public polyChildConfiguration()
{
Map<TestChild>(x => x.Requires("polyType").HasValue(1));
Map<OtherChild>(x => x.Requires("polyType").HasValue(2));
Hasrequired(x => x.Parent)
.WithMany(x => x.Children)
.Map(x => x.MapKey("polyParentId"));
}
}
这种关系是有效的,但是由于区分符在两种类型的模式中的工作方式,不同的父类型应仅包含相同匹配区分符的子级。这不是设计方案的方式,因为这不是真正的多态行为,也不是特别需要的,但这是我坚持要做的事情。现在的问题是,如果我有一个“ OneParent”类,则它应仅包含“ OneChild”类型的子代。但是,此建模使我可以将“ AnotherChild”类型的子代添加到“ OneParent”类型。
我宁愿定义如下内容:
[Table("polyParents")]
public abstract class polyParent
{
[Key]
public int polyParentId { get; set; }
public string Name { get; set; }
}
public class OneParent : polyParent
{
public virtual ICollection<OneChild> Children { get; set; } = new List<OneChild>();
}
public class AnotherParent : polyParent
{
public virtual ICollection<AnotherChild> Children { get; set; } = new List<AnotherChild>();
}
[Table("polyChildren")]
public abstract class polyChild
{
[Key]
public int polyChildId { get; set; }
public string Name { get; set; }
}
public class OneChild : polyChild
{
public virtual OneParent Parent { get; set; }
}
public class AnotherChild : polyChild
{
public virtual AnotherParent Parent { get; set; }
}
public class polyParentConfiguration : EntityTypeConfiguration<polyParent>
{
public polyParentConfiguration()
{
Map<TestParent>(x => x.Requires("polyType").HasValue(1));
Map<OtherParent>(x => x.Requires("polyType").HasValue(2));
}
}
public class polyChildConfiguration : EntityTypeConfiguration<polyChild>
{
public polyChildConfiguration()
{
Map<TestChild>(x => x.Requires("polyType").HasValue(1));
Map<OtherChild>(x => x.Requires("polyType").HasValue(2));
}
}
public class OneChildConfiguration : EntityTypeConfiguration<OneChild>
{
public OneChildConfiguration()
{
Hasrequired(x => x.Parent).WithMany(x => x.Children).Map(x => x.MapKey("polyParentId"));
}
}
public class AnotherChildConfiguration : EntityTypeConfiguration<AnotherChild>
{
public AnotherChildConfiguration()
{
Hasrequired(x => x.Parent).WithMany(x => x.Children).Map(x => x.MapKey("polyParentId"));
}
}
本质上是在子类而不是抽象基类中强力键入关系。但是,EF似乎并不接受这一点。上面产生了一个错误,它不能在polyChild上两次定义polyParentId。我还尝试在抽象的polyChild类中声明polyParentId,并在子类配置中使用.HasForeignKey()。但是,EF抱怨说在子类中找不到polyParentId键。 (即使它是继承的)。我还尝试将polyParentId放在每个子类中,但这似乎触发了EF将FK映射之一替换为不存在的“ polyParentId1”列。
我以为我会抛出这个问题,看看是否有什么方法尚未尝试在EF中映射此实体结构。我可能可以通过使用动作方法来修改解决方案中的状态来缓解松散类型关系的行为,以便突变方法仅接受正确的类型,但是我更喜欢如果集合/查询的类型更强。 / p>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)