Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This is my first project using EF and I'm struggling with entity relationships. I have a database that has two tables that are set up something like this:

Table Keys

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. When I make the join in the model view EF adds the other key fields to the join and guesses at the related fields. If I don't join the tables, I get an error

Problem in mapping fragments starting at line 294:No mapping specified for properties

and

Problem in mapping fragments starting at line 254:Potential runtime violation of table PARTDETAILS keys

Am I doing something wrong? I found this SO post which indicates this may not be possible. If it's not possible, what is the best way to handle situations like this?

like image 559
mack Avatar asked Dec 31 '25 02:12

mack


1 Answers

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.

like image 171
Jeff Dunlop Avatar answered Jan 02 '26 15:01

Jeff Dunlop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!