简体   繁体   English

在 Entity Framework Core 中为没有显式外键的表添加自定义导航属性

[英]Adding custom navigation property in Entity Framework Core for table that does not have an explicit foreign key

I have 2 separate models that logically are one-to-many but there is no explicit foreign key relationship.我有 2 个独立的模型,它们在逻辑上是一对多的,但没有明确的外键关系。

Table1 has a one-to-many with Table2 Table1 与 Table2 一对多

The models are as follows:型号如下:

public class Table1
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}
}

public class Table2
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}
    public string RelatedTableName {get; set;}
    public string? RelatedTableId {get; set;}
}

Table2 would, for the time being, have "Table1" and "10" as the RelatedTableName and RelatedTableId respectively. Table2 暂时将“Table1”和“10”分别作为 RelatedTableName 和 RelatedTableId。 In the future, there could potentially be a Table3 that also has a one-to-many with Table2 in the same way.将来,可能会有一个 Table3 也以同样的方式与 Table2 一对多。

Is there any way, using fluent API or anything else, that would allow EF to understand this relationship?有什么办法,使用流利的 API 或其他任何东西,可以让 EF 理解这种关系?

The reason I'm asking is because a record in Table1 is created at the same time as the records in Table2.我问的原因是因为 Table1 中的记录与 Table2 中的记录同时创建。 Normally, to associate these records, you would add them to each other's navigation properties as Table1's Id has not yet been generated.通常,为了关联这些记录,您会将它们添加到彼此的导航属性中,因为尚未生成 Table1 的 Id。 In this example, it does not seem so straightforward to do that.在此示例中,这样做似乎并不那么简单。

There is another workaround that I can take - saving the first entity before the second.我可以采取另一种解决方法 - 在第二个实体之前保存第一个实体。 I'd prefer not to do this.我宁愿不这样做。

EF is essentially a mapping between your C# model and the DB Schema. EF 本质上是 C# model 和 DB Schema 之间的映射。 What you have asked for cannot be done in the Database (a single column that can be a foreign key for multiple tables).您要求的内容无法在数据库中完成(单个列可以是多个表的外键)。 If the underlying Database cannot support this, then EF can't support as well.如果底层数据库不能支持这个,那么 EF 也不能支持。

There are two possible workarounds I can think of:我能想到两种可能的解决方法:

First:第一的:

public class Table2
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}

    public string RelatedTableName {get; set;}
    public int? Table1RowId {get; set;}
    public int? Table2RowId {get; set;}
    // ...
    public int? Table10RowId {get; set;}
}

Second第二

You can have subclasses for Table2, each having a foreign key for one of the tables.您可以有 Table2 的子类,每个子类都有一个表的外键。 See here for how to implement inheritance in EF.请参阅此处了解如何在 EF 中实现 inheritance。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有复合外键的实体框架导航属性 - Entity Framework Navigation Property with Composite Foreign Key 在实体框架中将表单导航属性迁移到外键 - Migrating form Navigation Property to Foreign Key in Entity Framework 实体框架可以将同一列映射到导航属性和外键吗 - Can Entity Framework map the same column to a Navigation Property and a Foreign Key 实体框架,更改外键,但导航属性不变 - entity framework, change foreign key but navigation property not changing 没有导航属性的外键实体框架数据注释 - Entity framework data annotation of foreign key without a navigation property 实体框架 - 未设置外键(0 / null)但导航属性不为空 - Entity Framework - Foreign Key not set (0 / null) but navigation property is not null 使用UserName作为实体框架中的外键创建ApplicationUser导航属性 - Creating an ApplicationUser navigation property using UserName as the foreign key in Entity Framework 如何在没有反向导航属性的实体框架中配置外键? - How to configure foreign key in Entity Framework without an inverse navigation property? 实体框架-访问本身具有外键/导航键的模型类 - Entity Framework - Accessing Model classes that have foreign/navigation key to themselves 实体框架核心迁移构建器未在外键上设置可为空的属性 - Entity Framework Core Migrationbuilder not setting nullable property on foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM