[英]Entity Framework Relationships on Non-Primary Keys
I am using VS 2010 with .Net version 4.0 and EF version 5 with an existing database. 我使用VS 2010与.Net版本4.0和EF版本5与现有数据库。 This is my first project using EF and I'm struggling with entity relationships.
这是我使用EF的第一个项目,我正在与实体关系进行斗争。 I have a database that has two tables that are set up something like this:
我有一个数据库,有两个表设置如下:
I simply want to join them in EF as a One-To-Many relationship joined on PART_SEQ_ID alone so that I can use LINQ to query. 我只是想将它们加入到EF作为一对多关系,仅在PART_SEQ_ID上加入,以便我可以使用LINQ进行查询。 When I make the join in the model view EF adds the other key fields to the join and guesses at the related fields.
当我在模型视图中进行连接时,EF会将其他关键字段添加到连接中,并在相关字段中进行猜测。 If I don't join the tables, I get an error
如果我不加入表格,我会收到错误
Problem in mapping fragments starting at line 294:No mapping specified for properties
从第294行开始映射片段的问题:没有为属性指定映射
and 和
Problem in mapping fragments starting at line 254:Potential runtime violation of table PARTDETAILS keys
从第254行开始映射片段的问题:潜在的运行时违反表PARTDETAILS键
Am I doing something wrong? 难道我做错了什么? I found this SO post which indicates this may not be possible.
我发现这个SO帖子表明这可能是不可能的。 If it's not possible, what is the best way to handle situations like this?
如果不可能,那么处理这种情况的最佳方法是什么?
I don't think you're going to get navigators to work with your schema as it is. 我认为你不会让导航员按原样使用你的架构。 Either you would change your schema so that each table has a unique, immutable, single-column primary key, or you would manage the joins in your query:
您可以更改架构,以便每个表都有唯一的,不可变的单列主键,或者您将在查询中管理连接:
from detail in partdetails
join part in parts on detail.part_seq_id equals part.part_seq_id...
Pay attention to the generated sql and look at your execution plan to ensure you have the needed indexes to efficiently construct your composition. 注意生成的sql并查看您的执行计划,以确保您拥有所需的索引来有效地构建您的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.