在没有 PK 的情况下映射实体框架中的属性?

发布于 2024-11-25 05:04:24 字数 769 浏览 0 评论 0原文

我尝试创建一个带有 3 个表的 EF 模型(VS2010,.NET4),每个表都有一个名为 Sync_ID 的 PK。

TABLE_HEAD:(Sync_ID(PK)、GRID_ID int、SERIALNUMBER int、YEAR int)

TABLE_POINT:(Sync_ID(PK)、GRID_ID int、SERIALNUMBER int、YEAR int、POINT_NUMBER int)

TABLE_PLANT:(Sync_ID(PK)、GRID_ID int、SERIALNUMBER int) , 年int,POINT_NUMBER int,PLANT_NUMBER int)

关联: TABLE_HEAD "1 To Many" TABLE_POINT "1 To Many" TABLE_PLANT

如您所见,TABLE_HEAD 的 GRID_ID、SERIALNUMBER、YEAR 应该映射到 GRID_ID、SERIALNUMBER、TABLE_POINT 的 YEAR

和 GRID_ID、SERIALNUMBER , TABLE_POINT 的 YEAR 应该映射到 GRID_ID、SERIALNUMBER、YEAR、POINT_NUMBER

我的第一种方法是使用 GRID_ID、SERIALNUMBER、YEAR 分组为 PK,但这会违反条件。

所以唯一的办法就是使用Sync_ID作为PK。但是如何才能像上面描述的那样映射其他字段呢?我可以只映射 PK_Columns 吗?

希望你有想法可以帮助我。也欢迎其他方法。 此致 !

I tried to create a EF-Model(VS2010, .NET4) with 3 Tables, each Table with a PK called Sync_ID.

TABLE_HEAD: (Sync_ID (PK), GRID_ID int, SERIALNUMBER int, YEAR int)

TABLE_POINT: (Sync_ID (PK), GRID_ID int, SERIALNUMBER int, YEAR int,POINT_NUMBER int)

TABLE_PLANT: (Sync_ID (PK), GRID_ID int, SERIALNUMBER int, YEAR int,POINT_NUMBER int,PLANT_NUMBER int)

Associations: TABLE_HEAD "1 To Many" TABLE_POINT "1 To Many" TABLE_PLANT

As you can see the GRID_ID, SERIALNUMBER, YEAR of TABLE_HEAD should be mapped to GRID_ID, SERIALNUMBER, YEAR of TABLE_POINT

And GRID_ID, SERIALNUMBER, YEAR of TABLE_POINT should be mapped to GRID_ID, SERIALNUMBER, YEAR,POINT_NUMBER

My first approach was to use GRID_ID, SERIALNUMBER, YEAR grouped as a PK, but that would be a violation of the Condition.

So the only way is to use the Sync_ID as PK. But how is it possible to map the other field as described above? Can I only map PK_Columns?

Hope you have ideas to help me. Other approaches are welcome, too .
best regards !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-12-02 05:04:24

其他方法是创建正确的数据库结构!

您将 Sync_ID 作为 PK,因此在依赖实体中将其用作 FK,而不是做您所描述的混乱。这与正确的数据库架构没有任何共同点。关系数据库的要点是最大限度地减少数据重复,但是您将几乎所有数据从第一个表复制到所有依赖表。此外:

  • 如果 TABLE_HEAD 没有 GRID_IDSERIALNUMBERYEAR 的唯一索引,则它不能是一对多关系的主体实体
  • 。如果它具有唯一索引,那并不重要,因为 EF 尚不支持唯一索引,因此它必须是主键才能作为主体实体参与关联

。您应该阅读有关数据库规范化的内容。

Other approach is create correct database structure!!!

You have Sync_ID as PK so use it as FK in dependent entity instead of doing a mess you have described. That has nothing in common with correct DB architecture. The point of relational database is to minimize data duplicity but you copy almost all your data from the first table to all dependent tables. Moreover:

  • If TABLE_HEAD doesn't have unique index for GRID_ID, SERIALNUMBER, YEAR it cannot be principal entity of one-to-many relation
  • Even if it has the unique index it doesn't matter because EF doesn't support unique indices yet so it must be primary key to participate as principal entity in association

You should read something about database normalization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文