如何解决可以为相关子类“收紧” TPH吗?
我遇到的情况是,现有的数据库结构使用了适合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 举报,一经查实,本站将立刻删除。