Data Annotation Feature -- ForeignKey, annotation -- foreignkey

Source: Internet
Author: User

Data Annotation Feature -- ForeignKey, annotation -- foreignkey

The foreign key feature can be applied to class attributes. The default Code-First Convention assumes that the name of the foreign key attribute matches the primary key attribute.

Let's take a look at the following code:

using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{    [Table("StudentMaster",Schema="WaHaHa")]   public class Student    {        [Key]        [Column(Order=5)]        public int StudentKey1 { get; set; }        [Key]        [Column(Order=6)]        public int StudentKey2 { get; set; }                [MaxLength(20)]        [ConcurrencyCheck]        [Required]        [Column("SName",Order=1,TypeName="nvarchar")]        public string StudentName { get; set; }        [NotMapped()]        public int? Age { get; set; }              public int StdId { get; set; }        [ForeignKey("StdId")]        public virtual Standard Standard { get; set; }          }}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Text;namespace EF2{   public class Standard    {       [Key]       public int StdId { get; set; }       public string StandardName { get; set; }    }}

As shown in the preceding figure, StdId is a foreign key in Student table and a primary key in Standard table.

Whether to use the class name + id or not.

By default, this is used:

Using System; using System. collections. generic; using System. componentModel. dataAnnotations; using System. componentModel. dataAnnotations. schema; using System. linq; using System. text; using System. threading. tasks; namespace EF2 {[Table ("StudentMaster", Schema = "WaHaHa")] public class Student {[Key] [Column (Order = 5)] public int StudentKey1 {get; set;} [Key] [Column (Order = 6)] public int StudentKey2 {get; s Et;} [MaxLength (20)] [ConcurrencyCheck] [Required] [Column ("SName", Order = 1, TypeName = "nvarchar")] public string StudentName {get; set;} [NotMapped ()] public int? Age {get; set;} public int StandardId {get; set;} // class name + ID [ForeignKey ("StandardId")] public virtual Standard {get; set ;}}}
Using System; using System. collections. generic; using System. componentModel. dataAnnotations; using System. linq; using System. text; namespace EF2 {public class Standard {[Key] public int StandardId {get; set;} // class name + ID public string StandardName {get; set ;}}}

 

Take a look at the following:

using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{    [Table("StudentMaster",Schema="WaHaHa")]   public class Student    {        [Key]        [Column(Order=5)]        public int StudentKey1 { get; set; }        [Key]        [Column(Order=6)]        public int StudentKey2 { get; set; }                [MaxLength(20)]        [ConcurrencyCheck]        [Required]        [Column("SName",Order=1,TypeName="nvarchar")]        public string StudentName { get; set; }        [NotMapped()]        public int? Age { get; set; }        public int StandardRefId { get; set; }        [ForeignKey("StandardRefId")]        public virtual Standard Standard { get; set; }          }}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Text;namespace EF2{   public class Standard    {       [Key]       public int StandardId { get; set; }       public string StandardName { get; set; }    }}

View database:

 

This means that we can randomly specify the foreign key name, haha, haha...

Related Article

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.