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

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

看看下面的:

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的更多相关文章

  1. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

  2. 7.7 数据注解特性--Table

    大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...

  3. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  4. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  5. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  6. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  7. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  8. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  9. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

随机推荐

  1. min.js反压缩

    给个网址自己体会.. http://jsbeautifier.org/ 当需要修改min.js中的代码时,把min.js文件ctrl+c   ctrl+v扔到上面的网页里,点击beautify 即可

  2. 反向传播(BP)算法

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:刘皮皮链接:https://www.zhihu.com/question/24827633/answer/29120394来源 ...

  3. Unity学习疑问记录之 动作动画忽略timeScale

    http://www.bubuko.com/infodetail-968864.html

  4. 《CLR.via.C#第三版》第二部分第6,7章节读书笔记(三)

    第6章讲的是类型和成员基础 重要认知:虚方法 虚方法的设计原则:设计一个类型时,应尽量减少所定义的虚方法的数量. 首先,调用虚方法的速度比调用非虚方法慢. 其次,JIT编译器不能内嵌虚方法,这进一步影 ...

  5. java中文乱码解决之道(七)-----JSP页面编码过程

    我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...

  6. 《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第六章  继承与建模高级应用 现在,你应该对实体框架中基本的建模有了一定的了解,本章 ...

  7. distribution 中一直在运行 waitfor delay @strdelaytime 语句

    Replication 自动创建来一个 Job:Replication monitoring refresher for distribution,这个Agent执行一个sp: dbo.sp_repl ...

  8. Razor视图出现重复的解决方法

  9. 【源码】谷歌代理~WPF简单小软件-2015-10-15首发 (2016-03-01已更新源)

    蛋疼,昨天把代理去了后才发现,原来咱们连谷歌应用都访问不了,,,用别人的总觉得不怎么安全,然后今天早上就编了个小软件干掉他这限制==>  GoogleProxy.exe [主要目的:为了能在线安 ...

  10. [汇编与C语言关系]4. 结构体和联合体

    用反汇编的方法研究一下C语言的结构体: #include <stdio.h> int main(int argc, char ** argv) { struct { char a; sho ...