locked
How do i set a scalar property to be a foreign key using Model First? RRS feed

  • Question

  • Hi all.

    I was going through this EF tutorial: and wanted to recreate it to test the code. After i added the a new entity "StudentAddress" and added a scalar property "StudentID", using the edmx designer on visual studio 2013, how to i set "StudentID" to be represented as a foreign key to the entity "Student"?

    • Moved by Bob Beauchemin Monday, August 4, 2014 4:08 PM Moved to the appropriate forum
    Monday, August 4, 2014 3:45 PM

Answers

  • When i do this, it works all the way to the end, but when adding data, it keeps on screaming at me that i need to expose foreign key properties. So, i set the associations to 1-0..1,(with studentid on the StudentAddress not set to create it as key property) then.

    I then right clicked on the "StudentId" on the StudentAddress entity and selected properties. Then i set EntityKey to true (but not an identity).

    Then, i double clicked the association, set the principal to Student on the Referential Constraint Dialog Box.


    There is never infinite resources.

    • Marked as answer by KCWamuti Wednesday, August 6, 2014 1:03 PM
    Tuesday, August 5, 2014 6:07 AM

All replies

  • Hello,

    >>how to i set "StudentID" to be represented as a foreign key to the entity "Student"?

    From the link you provided, it is to create a one to one or zero relationship, for this, righting click the Student entity -> Add New -> Assocation -> Set Multiplicity to one on both sides:

    If this does not work for you, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 5, 2014 4:21 AM
  • When i do this, it works all the way to the end, but when adding data, it keeps on screaming at me that i need to expose foreign key properties. So, i set the associations to 1-0..1,(with studentid on the StudentAddress not set to create it as key property) then.

    I then right clicked on the "StudentId" on the StudentAddress entity and selected properties. Then i set EntityKey to true (but not an identity).

    Then, i double clicked the association, set the principal to Student on the Referential Constraint Dialog Box.


    There is never infinite resources.

    • Marked as answer by KCWamuti Wednesday, August 6, 2014 1:03 PM
    Tuesday, August 5, 2014 6:07 AM