1

Suppose I have the following 3 tables in an Oracle DB:

TABLE1:

Table1Id    Field1    Field2    Field3

With Table1Id as the primary key, but Field1 also being unique and could be made a primary key, even though it isn't defined as such in my DB.

Table2:

Table2Id    Field1    Field4    Field5

With Table2Id as the primary key and Field1 matching the values from Field1 from Table1.

Table3:

Table3Id   Table1Id   Field6

With Table3Id as the primary key and Table1Id matching the value from Table1Id from Table1.


So, I create an entity framework 5 DB-first model (can't do EF 6 because it seems the Oracle model isn't supported yet in that version) and am able to easily create the association between Table3 and Table1, but now I want to create the association between Table2 and Table1.

I found that if I add Field1 as a primary key in Table1 (had to do so by editing the XML for the EDMX file since I otherwise get an error just using the GUI), then I get an issue setting the association since now that there are 2 primary keys, it wants me to map both to fields in Table2. If I leave the association from Table1 > Table1Id as blank and only fill in the association for Table1 > Field1 to Table2 > Field1, I get the a 111 Error.

I am still VERY new to Entity Framework... I hope I explained my question well, but I have no clue how to overcome this... I've seen some answers that say it can't be done in EF4, but is there a way in the later version?

Any help at all (please remember I'm an EF newbie!!) would REALLY be appreciated!!

Community
  • 1
  • 1
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • Can you add `Table1Id` to Table2? – jamesSampica Jun 26 '14 at 15:41
  • I, unfortunately, have no control over the source DB... I can query it, but **nothing else**... – John Bustos Jun 26 '14 at 15:54
  • It seems to me that you have to declare Field1 as a PK for Table2. You can then set a relation between table1 and table2. But if, in the same context, you need and relation Table1(Field1) <-> Table2(Field1) and Table2(Table2Id) <-> TableX(TableXId), then you are in trouble (except by writing linq query and set the relation by hand through joins) – tschmit007 Jun 26 '14 at 16:36
  • Yeah, thanks @tschmit007, that's exactly the problem... I simplified the problem, but each of these tables is connected to others via their corresponding primary keys... I was hoping there was something I didn't know in EF to do this... As fo rthe Linq solution, I'm guessing that's how I'll go, but then I'm no longer dealing with EF, right? – John Bustos Jun 26 '14 at 16:39
  • in my knowledge, at this time EF does not allow to address simply scenario with 2 PK for a table. I face similar case and, in my case, can use two contexts. – tschmit007 Jun 26 '14 at 16:41
  • 1
    if you are in read only, and can create a view, you can use a vTable2 instead of table and build vtable2 to expose Table1Id – tschmit007 Jun 26 '14 at 16:45
  • I like that idea! - Since I have no way to create the view in the DB itself, I used a defining query (http://msdn.microsoft.com/en-us/data/jj730307.aspx). I'm playing around with it now, but could / would this work?? – John Bustos Jun 26 '14 at 18:19

0 Answers0