简体   繁体   English

非主键上的实体框架关系

[英]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.

相关问题 非主要唯一实体不可在实体框架数据库中首先连接 - Non-Primary Unique Not Joinable in Entity Framework Database First 使用非主键列的实体框架自引用 - Entity Framework Self Referencing Using Non-Primary Key Column 使用 Entity Framework 4.1 Fluent API 在非主键字段上创建关联 - Create association on non-primary key fields with Entity Framework 4.1 Fluent API 实体框架 - 如何在辅助表中的非主键列上连接表? - Entity Framework - How do I join tables on non-primary key columns in secondary tables? 用于查找具有非主键值的实体的存储库方法 - Repository method to find an entity with a non-Primary Key value Entity Framework Core 2.1.14 DB First Missing Foreign Key on a misconfigured data type 的非主候选键 - Entity Framework Core 2.1.14 DB First Missing Foreign Key on non-primary candidate key of a misconfigured data type 实体框架:在关联中使用非主唯一键的替代解决方案 - Entity Framework: Alternate solution to using non primary unique keys in an association 在非主轴上滚动 - Scrolling on a non-Primary Axis 使用主表中的重命名字段和非主键创建实体关系 - Creating entity relationship with renamed fields and non-primary key in primary table EF 6非主键上与同一实体的代码优先多对多关系 - EF 6 Code-First Many to Many relationship with same entity on non-primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM