开发者

mapping Properties in Entity Framework without PK?

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 !


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜