zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(9.9):DataAnnotations

    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.

    Entity Framework code-first example

    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.

    Entity Framework code-first example

    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.

  • 相关阅读:
    DAY-4 Linux基础及常用命令(1)
    DAY-3 计算机基础之网络
    DAY-2 计算机基础之操作系统
    DAY-1 计算机基础
    梅花作品欣赏
    简洁大气网址(国外)跟设计大学的案例很像
    animate css3 应用的借鉴,一个同事写的JS
    漂亮的素材
    几个不错的素材站
    正式开始我的技术生涯
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644205.html
Copyright © 2011-2022 走看看