7.9 数据注解特性--ForeignKey

时间:2023-03-08 17:50:20

外键特性,可以应用到类的属性中。Code-First默认的约定,对外键属性来说,假定外键属性的名称和主键属性是匹配的。

我们看一下,下面的代码:

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=)]
        public int StudentKey1 { get; set; }

        [Key]
        [Column(Order=)]
        public int StudentKey2 { get; set; }

        [MaxLength()]
        [ConcurrencyCheck]
        [Required]
        [Column(,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; }
    }
}

7.9 数据注解特性--ForeignKey

从上面的图中,可以看出,StdId在Student表中是外键,在Standard表中是主键。

这里是不是用类名+id的做法,使用外键。

默认的是使用这样的:

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=)]
        public int StudentKey1 { get; set; }

        [Key]
        [Column(Order=)]
        public int StudentKey2 { get; set; }

        [MaxLength()]
        [ConcurrencyCheck]
        [Required]
        [Column(,TypeName="nvarchar")]
        public string StudentName { get; set; }

        [NotMapped()]
        public int? Age { get; set; }

        public int StandardId { get; set; }   //类名+ID

        [ForeignKey("StandardId")]
        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; }     //类名+ID

       public string StandardName { get; set; }
    }
}

7.9 数据注解特性--ForeignKey

看看下面的:

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=)]
        public int StudentKey1 { get; set; }

        [Key]
        [Column(Order=)]
        public int StudentKey2 { get; set; }

        [MaxLength()]
        [ConcurrencyCheck]
        [Required]
        [Column(,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; }
    }
}

看数据库:

7.9 数据注解特性--ForeignKey

这个意思就是,我们可以随便指定外键的名称,哈哈哈哈哈。。。