.net - EF Core ForeignKey-like 关系
问题描述
考虑两个类:
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;}
}
推荐阅读
- netcdf4 - dask.array.compute() 失败并出现 RuntimeError: NetCDF: HDF error
- c# - Xamarin GraphQlHttpClient 只是崩溃
- python - 如何保持 Python 函数的精度?
- reactjs - React 的重定向组件在 axios .then() 承诺中没有按预期工作
- clojure - 如何调整来自 next.jdbc 的 IReduceInit 以使用 cheshire 将 JSON 流式传输到使用 ring 的 HTTP 响应
- javascript - Marionette document.querySelector('#test') 返回 null 但这样做。$('#test') 不会
- javascript - 在 Hooks React 中使用静态变量
- wordpress - 你能告诉我为什么我的 Wordpress 网站显示白页吗?
- javascript - 如何在 JavaScript 中删除 [object HTMLParagraphElement]
- excel - 如何在 VBA for Excel 中声明(Dim)应用程序匹配的总和?