locked
Defining Foreign Keys RRS feed

  • Question

  • User1308732545 posted

    Hi All,

    I am new to Entity Framework.

    I have been given a existing project to enhance .

    I don't know where the foreign keys are defined ,I can't see anything in the source code , i can see classes which are tables.

    Please could anyone let me know where to check and how to define a foreign key in entity framework.

    Thanks

    Tuesday, July 1, 2014 8:12 AM

Answers

  • User541108374 posted

    Hi,

    i can see classes which are tables.

    Which leads me to interpretation that you're making use of Entity Framework Code First. If not please state so.

    For Code First:

    In the Entity Framework, navigation properties provide a way to navigate a relationship between two entity types. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). Code First infers relationships based on the navigation properties defined on your types.

    In addition to navigation properties, we recommend that you include foreign key properties on the types that represent dependent objects. Any property with the same data type as the principal primary key property and with a name that follows one of the following formats represents a foreign key for the relationship: ‘<navigation property name><principal primary key property name>’, ‘<principal class name><primary key property name>’, or ‘<principal primary key property name>’. If multiple matches are found then precedence is given in the order listed above. Foreign key detection is not case sensitive. When a foreign key property is detected, Code First infers the multiplicity of the relationship based on the nullability of the foreign key. If the property is nullable then the relationship is registered as optional; otherwise the relationship is registered as required.

    If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null. The multiplicity and cascade delete behavior detected by convention can be overridden by using the fluent API.

    In the following example the navigation properties and a foreign key are used to define the relationship between the Department and Course classes.

    public class Department 
    { 
        // Primary key 
        public int DepartmentID { get; set; } 
        public string Name { get; set; } 
     
        // Navigation property 
        public virtual ICollection<Course> Courses { get; set; } 
    } 
     
    public class Course 
    { 
        // Primary key 
        public int CourseID { get; set; } 
     
        public string Title { get; set; } 
        public int Credits { get; set; } 
     
        // Foreign key 
        public int DepartmentID { get; set; } 
     
        // Navigation properties 
        public virtual Department Department { get; set; } 
    }

    Taken from http://msdn.microsoft.com/en-us/data/jj679962.aspx

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 1, 2014 8:56 AM