考虑两个类:
public class EntA
{
public Guid Id { get; set; }
[ForeignKey("EntityId")]
public ICollection<TranslationValue> Translations { get; set; }
}
public class EntB
{
public Guid Id { get; set; }
[ForeignKey("EntityId")]
public ICollection<TranslationValue> Translations { get; set; }
}
然后我们有TranslationValue类:
public class TranslationValue : IEntity
{
public Guid Id { get; set; }
public Guid EntityId { get; set; }
public string Value { get; set; }
}
一切正常,除了一件事:我不能将外键作为TranslationValue中的条目.EntityId将引用EntA和EntB。
所以问题是:如何在不实际创建外键的情况下以与上面完全相同的方式定义关系。
我目前使用的解决方法是创建迁移,然后删除负责创建实际外键的部分。
所以问题是:如何在不实际创建外键的情况下以与上面完全相同的方式定义关系。
它是不可能的,因为一个外键如何映射到两个不同的表?您的TranslationValue
模型必须如下:
public class TranslationValue : IEntity
{
public Guid Id { get; set; }
public Guid EntAId { get; set; }
public Guid EntBId { get; set; }
public string Value { get; set; }
public EntA EntA {get; set;}
public EntB EntB {get; set;}
}