none
Entity Framework - Creating keys RRS feed

  • Question

  • If I create these following classes, once the Entity Framework creates my database why it creates the student_studentId key under Subjects table instead of in the Students table ?

    public class Student

        {
            public int StudentId { get; set; }
            public string Nome { get; set; }
            public List<Subject> Subjects { get; set; }
        }

        public class Subject
        {
            public int SubjectId { get; set; }
            public string Nome { get; set; }
        }

    Tables generated in the database:

    Students Table
    - column StudentId
    - column Name

    Subjects Table
    - column StudentId
    - column Name
    - column Student_StudentId

    Wednesday, August 6, 2014 4:04 PM

Answers

  • Hello,

    >>If I create these following classes, once the Entity Framework creates my database why it creates the student_studentId key under Subjects table instead of in the Students table?

    This is because your two entities are one to many relationship, since you do not specify a foreign key explicitly, it would automatically create the foreign key in database. The foreign key only exists in sub table(Subjects table in your calse) is a database rule.

    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.

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:53 PM
    Thursday, August 7, 2014 8:17 AM
    Moderator

All replies

  • Hi AGA,

    I think this thread is specific to Entity Framework forum. So I am helping you to move this thread directly into that forum. Thank you for your understanding.

    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.

    Thursday, August 7, 2014 3:25 AM
    Moderator
  • Hello,

    >>If I create these following classes, once the Entity Framework creates my database why it creates the student_studentId key under Subjects table instead of in the Students table?

    This is because your two entities are one to many relationship, since you do not specify a foreign key explicitly, it would automatically create the foreign key in database. The foreign key only exists in sub table(Subjects table in your calse) is a database rule.

    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.

    • Marked as answer by AGA Neto Monday, August 11, 2014 9:53 PM
    Thursday, August 7, 2014 8:17 AM
    Moderator