在没有 PK 的情况下映射实体框架中的属性?
我尝试创建一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其他方法是创建正确的数据库结构!
您将 Sync_ID 作为 PK,因此在依赖实体中将其用作 FK,而不是做您所描述的混乱。这与正确的数据库架构没有任何共同点。关系数据库的要点是最大限度地减少数据重复,但是您将几乎所有数据从第一个表复制到所有依赖表。此外:
GRID_ID
、SERIALNUMBER
、YEAR
的唯一索引,则它不能是一对多关系的主体实体。您应该阅读有关数据库规范化的内容。
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:GRID_ID
,SERIALNUMBER
,YEAR
it cannot be principal entity of one-to-many relationYou should read something about database normalization.