locked
How do I map a foreign key field from table to table when neither is a primary key? RRS feed

  • Question

  • I'm trying to edit a MVC/EF6 application and I cannot figure out how to create a context declaration in OnModelCreating.

    The problem is that I have 2 classes containing an Int key of the same name (Call it 'ForeignId'). I want one class to use the other class's field of the same name as a foreign key, even though this field is not a primary key in either class/table and it is NOT called 'Id'.

    None of the books and references I've looked at so far seem to cover this scenario.

    Wednesday, July 20, 2016 3:23 PM

Answers

  • >So you're saying that what I'm trying to do is utterly impossible in EF6? 

    Yes.  You can certainly have both the entities, but EF won't understand that table1.ForeignID is supposed to match table2.ForeignID unless you declare table2.ForeignID as the Key in EF (which may work fine).

    >By 'alternate key' I assume you mean alternatelynamed key

    No I mean "alternate key", which means any key not declared as the primary key in the database.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Wednesday, July 20, 2016 5:06 PM

All replies

  • Please post a simple complete repro of your issue.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, July 20, 2016 3:26 PM
  • My apologies.  I can't.  I don't know how this program works and I have no documentation.  I have very little experience in EF.

    All I know is that they're using the OnModelCreating to set up a batch of relationships and the scenario I've described above is not present. 

    I've already posted what I think is a fair description here ( https://social.msdn.microsoft.com/Forums/en-US/be7acab4-0716-4261-92df-d94aaa401228/basic-theory-question-regarding-mapping-of-foreign-keys?forum=adodotnetentityframework )

    I was hoping that restating my problem might get me a faster answer.  I'm extremely short on time.

    Wednesday, July 20, 2016 3:51 PM
  • >I've already posted what I think is a fair description here

    But are you experiencing some sort of error?

    >So as yet I don't know how to declare a foreign key relationship when I need to look at a different field in the child table

    In EF6 you must declare exactly one key for an entity, and EF can only use Associations that point to that Key.  In SQL Server you can have a FK point to an alternate key, but not in EF6.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, July 20, 2016 4:34 PM
  • >But are you experiencing some sort of error?

    Yes, the system is matching the table1 ForeignId field to the Id field of table2 rather than the Table2 ForeignId field.

    >In EF6 you must declare exactly one key for an entity, and EF can only use Associations that point to that Key.  In SQL Server you can have a FK point to an alternate key, but not in EF6.

    So you're saying that what I'm trying to do is utterly impossible in EF6? 

    (By 'alternate key' I assume you mean alternately named key, a name other than 'Id'?)

    Wednesday, July 20, 2016 4:55 PM
  • >So you're saying that what I'm trying to do is utterly impossible in EF6? 

    Yes.  You can certainly have both the entities, but EF won't understand that table1.ForeignID is supposed to match table2.ForeignID unless you declare table2.ForeignID as the Key in EF (which may work fine).

    >By 'alternate key' I assume you mean alternatelynamed key

    No I mean "alternate key", which means any key not declared as the primary key in the database.

    David


    David http://blogs.msdn.com/b/dbrowne/


    Wednesday, July 20, 2016 5:06 PM