Data annotation Feature--foreignkey

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.