实体框架6 - 缺少表,只有主键引用不同的表

时间:2022-10-05 14:55:04

We are learning Entity Framework 6.1 (from NuGet) as we move away from Linq2Sql. We have a small handful of tables that associate two separate tables like shown below.

当我们离开Linq2Sql时,我们正在学习Entity Framework 6.1(来自NuGet)。我们有一些表关联两个独立的表,如下所示。

EF6 Database First generation

EF6数据库第一代

DB Diagram:

实体框架6  - 缺少表,只有主键引用不同的表

Schema Overview:

实体框架6  - 缺少表,只有主键引用不同的表

When in Visual studios, blank class library, doing a Database First EF6 EDMX file, the diagram only generates TableA and TableC -- the TableB does not get generated.

在Visual Studio,空白类库中,执行Database First EF6 EDMX文件时,该图只生成TableA和TableC - TableB不会生成。

实体框架6  - 缺少表,只有主键引用不同的表

Visual Studios View:

视觉工作室查看:

实体框架6  - 缺少表,只有主键引用不同的表

You can see that only TableA and TableC are created. Technically TableB should have been created, because you would want to be able to manage those references.

您可以看到只创建了TableA和TableC。从技术上讲,应该已经创建了TableB,因为您希望能够管理这些引用。

The Association between A and C shown in the diagram:

图中所示的A和C之间的关联:

实体框架6  - 缺少表,只有主键引用不同的表

I feel like I am missing an option, or misunderstanding a key concept of Entity Framework. Any idea how to have the missing TableB generated with the T4? The EDMX file does show it, but for some reason it doesn't get generated into a .CS file with the two properties indicating the relationship.

我觉得我错过了一个选项,或者误解了实体框架的一个关键概念。知道如何用T4生成丢失的TableB吗? EDMX文件确实显示了它,但由于某种原因,它不会生成到.CS文件中,其中两个属性指示关系。

The primary reason we need this, is we extended the EF6 T4 template to add some factory patterns to match our existing models. Because it doesnt generate a class for TableB, we dont get the autogenerated code that we are looking for.

我们需要这个的主要原因是我们扩展了EF6 T4模板以添加一些工厂模式以匹配我们现有的模型。因为它不会为TableB生成一个类,所以我们不会得到我们正在寻找的自动生成的代码。

Thoughts / suggestions? Thanks.

想法/建议?谢谢。

2 个解决方案

#1


6  

Weak entities or join tables will not be generated by EF, you need to configure the relationships manually thru fluent API or using data annotations

EF不会生成弱实体或连接表,您需要通过流畅的API手动配置关系或使用数据注释

As stated on Microsoft's website: under Relationship's convention:

正如微软网站上所述:根据关系惯例:

Note: If you have multiple relationships between the same types (for example, suppose you define the Person and Book classes, where the Person class contains the ReviewedBooks and AuthoredBooks navigation properties and the Book class contains the Author and Reviewer navigation properties) you need to manually configure the relationships by using Data Annotations or the fluent API. For more information, see Data Annotations - Relationships and Fluent API - Relationships.

注意:如果相同类型之间存在多个关系(例如,假设您定义了Person和Book类,其中Person类包含CommentsBooks和AuthoredBooks导航属性,而Book类包含Author和Reviewer导航属性),则需要使用Data Annotations或Fluent API手动配置关系。有关更多信息,请参阅数据注释 - 关系和Fluent API - 关系。

Refer to this link for more information

有关更多信息,请参阅此链接

UPDATED

A workaround will work in case of EDMX ( but it cost maintenance) as follows:

对于EDMX(但需要维护费用),解决方法可以如下工作:

  1. Remove the foreign keys from the join table in the database
  2. 从数据库中的连接表中删除外键

  3. Update the EDMX from database
  4. 从数据库更新EDMX

  5. Recreate the foreign keys in the join table
  6. 在连接表中重新创建外键

this workaround will work as long as you will not update your Model from the database again.

只要您不再次从数据库更新模型,此解决方法就会起作用。

Recommended solution, keep everything as it was generated by EDMX and learn more about how to use crud operation for this case using the following links that were reported "helpful" by the user '@TravisWhidden'

建议的解决方案,保留EDMX生成的所有内容,并使用以下链接了解有关如何使用crud操作的更多信息,这些链接由用户“@TravisWhidden”报告为“有用”

  1. Insert/Update Many to Many Entity Framework . How do I do it?
  2. 插入/更新多个到多个实体框架。我该怎么做?

  3. https://www.youtube.com/watch?v=uMQwORSTGX4 ( video)
  4. https://www.youtube.com/watch?v=uMQwORSTGX4(视频)

#2


2  

As mentioned by @Hadi Hassan, EF will not “Expose” or recognize Relational Tables that are composed exclusively of other Entities.

正如@Hadi Hassan所提到的,EF不会“公开”或识别仅由其他实体组成的关系表。

Work Around:
If you only need to ‘READ’ the data you can

解决方法:如果您只需要“读取”您可以获得的数据

  1. Define a view in your Schema for TableB.
  2. 在Schema for TableB中定义一个视图。

  3. Then do a Model (.EDMX) Update from DB (select the Update Views)
  4. 然后从DB执行模型(.EDMX)更新(选择更新视图)

  5. You will now be able to query your TableB data using your EF Context.
  6. 您现在可以使用EF上下文查询TableB数据。

If you need to modify (Create,Update,Destroy) records in your TableB

如果需要在TableB中修改(创建,更新,销毁)记录

  1. Create Stored Procedures in your Schema, accordingly.

    相应地在架构中创建存储过程。

  2. Import your Procs as Function into Your EF Model

    将Procs作为函数导入EF模型

  3. You can now call those Functions from your model Context for the rest of your CRUD operations.

    现在,您可以从模型上下文中调用这些函数,以用于其余的CRUD操作。

#1


6  

Weak entities or join tables will not be generated by EF, you need to configure the relationships manually thru fluent API or using data annotations

EF不会生成弱实体或连接表,您需要通过流畅的API手动配置关系或使用数据注释

As stated on Microsoft's website: under Relationship's convention:

正如微软网站上所述:根据关系惯例:

Note: If you have multiple relationships between the same types (for example, suppose you define the Person and Book classes, where the Person class contains the ReviewedBooks and AuthoredBooks navigation properties and the Book class contains the Author and Reviewer navigation properties) you need to manually configure the relationships by using Data Annotations or the fluent API. For more information, see Data Annotations - Relationships and Fluent API - Relationships.

注意:如果相同类型之间存在多个关系(例如,假设您定义了Person和Book类,其中Person类包含CommentsBooks和AuthoredBooks导航属性,而Book类包含Author和Reviewer导航属性),则需要使用Data Annotations或Fluent API手动配置关系。有关更多信息,请参阅数据注释 - 关系和Fluent API - 关系。

Refer to this link for more information

有关更多信息,请参阅此链接

UPDATED

A workaround will work in case of EDMX ( but it cost maintenance) as follows:

对于EDMX(但需要维护费用),解决方法可以如下工作:

  1. Remove the foreign keys from the join table in the database
  2. 从数据库中的连接表中删除外键

  3. Update the EDMX from database
  4. 从数据库更新EDMX

  5. Recreate the foreign keys in the join table
  6. 在连接表中重新创建外键

this workaround will work as long as you will not update your Model from the database again.

只要您不再次从数据库更新模型,此解决方法就会起作用。

Recommended solution, keep everything as it was generated by EDMX and learn more about how to use crud operation for this case using the following links that were reported "helpful" by the user '@TravisWhidden'

建议的解决方案,保留EDMX生成的所有内容,并使用以下链接了解有关如何使用crud操作的更多信息,这些链接由用户“@TravisWhidden”报告为“有用”

  1. Insert/Update Many to Many Entity Framework . How do I do it?
  2. 插入/更新多个到多个实体框架。我该怎么做?

  3. https://www.youtube.com/watch?v=uMQwORSTGX4 ( video)
  4. https://www.youtube.com/watch?v=uMQwORSTGX4(视频)

#2


2  

As mentioned by @Hadi Hassan, EF will not “Expose” or recognize Relational Tables that are composed exclusively of other Entities.

正如@Hadi Hassan所提到的,EF不会“公开”或识别仅由其他实体组成的关系表。

Work Around:
If you only need to ‘READ’ the data you can

解决方法:如果您只需要“读取”您可以获得的数据

  1. Define a view in your Schema for TableB.
  2. 在Schema for TableB中定义一个视图。

  3. Then do a Model (.EDMX) Update from DB (select the Update Views)
  4. 然后从DB执行模型(.EDMX)更新(选择更新视图)

  5. You will now be able to query your TableB data using your EF Context.
  6. 您现在可以使用EF上下文查询TableB数据。

If you need to modify (Create,Update,Destroy) records in your TableB

如果需要在TableB中修改(创建,更新,销毁)记录

  1. Create Stored Procedures in your Schema, accordingly.

    相应地在架构中创建存储过程。

  2. Import your Procs as Function into Your EF Model

    将Procs作为函数导入EF模型

  3. You can now call those Functions from your model Context for the rest of your CRUD operations.

    现在,您可以从模型上下文中调用这些函数,以用于其余的CRUD操作。