Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

DataAnnotations - ForeignKey Attribute:

ForeignKey attribute can be applied to properties of a class. Default Code-First convention for ForeignKey relationship expects foreign key property name match with primary key property.

Consider the following example.

public class Student
{
    public Student()
    { 

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }

    //Foreign key for Standard
    public int StandardId { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public ICollection<Student> Students { get; set; }

 }

As you can see in the above code, Student class includes foreign key StandardId, which is key property in Standard class. So now, Code First will create StandardId column in Students class as shown below.

ForeignKey attribute overrides this convention. You can have a different name of a foreign key property name than the primary key of a dependent class.

public class Student
{
    public Student()
    { 

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }

    //Foreign key for Standard
    public int StandardRefId { get; set; }

    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public ICollection<Student> Students { get; set; }

}

As you can see in the above example, Student class includes StandardRefId foreign key property name instead of StandardId. We specify it using ForeignKey attribute on Standard navigation property so that Code-First will consider StandardRefId as foreign key, as shown below.

ForeignKey attribute can be applied to key property to specify which navigation property it points to, as shown below.

public class Student
{
    public Student()
    { 

    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }

    //Foreign key for Standard

    [ForeignKey("Standard")]
    public int StandardRefId { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 

    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public ICollection<Student> Students { get; set; }

}

Thus, you can use ForeignKey attribute to give a different name to a foreign key property.

时间: 2024-08-02 11:20:54

Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute的相关文章

Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Default Code First convention creates a column name same as the property name. Column attribute overrides this default convention. EF Code-First will create

Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array property of a domain class. TimeStamp attribute creates a column with timestamp datatype. Code-First automatically use this TimeStamp column in concurren

Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First convention creates a table name same as the class name. Table attribute overrides this default convention. EF Code-First will create a table with a speci

Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. Default Code-First convention creates a column for all the properties which includes getters and setters. NotMapped attribute overrides this default c

Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code-First convention creates a primary key column for a property whose name is "Id" or {Class Name} + "Id". Key attribute overrides this d

Entity Framework Code-First(9.4):DataAnnotations - Required Attribute

Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT NULL column in a database table for a property on which we apply Required attribute. Note that it can also be used with ASP.Net MVC as a validation att

Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute

DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that Code-First creates {Class Name}_{Primary Key} foreign key column if you have not included foreign key property in a parent class. The InverseProperty

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Entity Framework Code First (二)Custom Conventions

------------------------------------------------------------------------------------------------------------ 注意:以下所讨论的功能或 API 等只针对 Entity Framework 6 ,如果你使用早期版本,可能部分或全部功能不起作用! --------------------------------------------------------------------------