A foreign key attribute that can be applied to the properties of a class. Code-first The default convention, the foreign key attribute assumes that the name of the foreign key property and the primary key attribute are matched.
Let's take a look at the following code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef2{[Table ("Studentmaster", schema="Wahaha")] Public classStudent {[Key] [Column (Order=5)] Public intStudentKey1 {Get;Set; } [Key] [Column (Order=6)] Public intStudentKey2 {Get;Set; } [MaxLength ( -] [Concurrencycheck] [Required] [Column ("SName", order=1, typename="nvarchar")] Public stringStudentname {Get;Set; } [Notmapped ()] Public int? Age {Get;Set; } public int StdId {get; set;} [ForeignKey ("StdId")] public Vsan standard {get; set; } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingSystem.Text;namespaceef2{ Public classStandard { [Key] public int StdId {get; set;} Public stringStandardName {Get;Set; } }}
As you can see from the above figure, stdID is a foreign key in the student table and a primary key in the standard table.
Here is not the practice of using the class name +id, the foreign key is used.
The default is to use this:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef2{[Table ("Studentmaster", schema="Wahaha")] Public classStudent {[Key] [Column (Order=5)] Public intStudentKey1 {Get;Set; } [Key] [Column (Order=6)] Public intStudentKey2 {Get;Set; } [MaxLength ( -] [Concurrencycheck] [Required] [Column ("SName", order=1, typename="nvarchar")] Public stringStudentname {Get;Set; } [Notmapped ()] Public int? Age {Get;Set; } public int Standardid {get; set;} Class name +id [ForeignKey ("Standardid")] public Vsan standard {get; set; } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingSystem.Text;namespaceef2{ Public classStandard { [Key] public int Standardid {get; set;} Class name +id Public stringStandardName {Get;Set; } }}
Look at the following:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceef2{[Table ("Studentmaster", schema="Wahaha")] Public classStudent {[Key] [Column (Order=5)] Public intStudentKey1 {Get;Set; } [Key] [Column (Order=6)] Public intStudentKey2 {Get;Set; } [MaxLength ( -] [Concurrencycheck] [Required] [Column ("SName", order=1, typename="nvarchar")] Public stringStudentname {Get;Set; } [Notmapped ()] Public int? Age {Get;Set; } public int Standardrefid {get; set;} [ForeignKey ("Standardrefid")] public Vsan standard {get; set; } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.Linq;usingSystem.Text;namespaceef2{ Public classStandard { [Key] public int Standardid {get; set;} Public stringStandardName {Get;Set; } }}
Look at the database:
This means that we can casually specify the name of the foreign key, hahaha haha ...
Data annotation Feature--foreignkey