7.9 数据注解特性--ForeignKey

外键特性,可以应用到类的属性中。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=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; }
    }
}

从上面的图中,可以看出,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=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 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; }
    }
}

 

看看下面的:

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; }
    }
}

看数据库:

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值