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

EF 6 Code-First系列文章目录:

当两个实体之间不止一种关系的时候,可以使用InverseProperty特性,为了理解InverseProperty特性我们来做一个例子:
1.创建一个控制台应用程序,安装好EF:

2.创建Course类:

 public class Course
{
public int CourseId { get; set; } public string CourseName { get; set; } public string Description { get; set; } public Teacher OnlineTeacher { get; set; }
}

3.创建Teacher类:

public class Teacher
{
public int TeacherId { get; set; } public string Name { get; set; } public ICollection<Course> OnlineCourses { get; set; }
}

上面的代码例子中,Course和Teacher实体之间是一对多的关系,一个Teacher可以教很多的Online Course。
3.创建上下文类:

public class EFDbContext:DbContext
{
public EFDbContext()
: base("name=Constr")
{ } public DbSet<Course> Courses { get; set; } public DbSet<Teacher> Teachers { get; set; }
}

4.SQL链接字符串:

<!--SQL链接字符串-->
<connectionStrings>
<add name="Constr" connectionString="Server=.;Database=EFAnnotationInversePropertyDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/>
</connectionStrings>

5.测试代码:

 class Program
{
static void Main(string[] args)
{
using (var db = new EFDbContext())
{
List<Course> lstModel = db.Courses.ToList();
}
Console.WriteLine("success");
Console.ReadKey();
}
}

6.运行程序:

可以看到,根据默认的约定,生成的数据库如下:

现在假设Course和Teacher实体之间,还有一个一对多的关系:
Course类:

 public class Course
{
public int CourseId { get; set; } public string CourseName { get; set; } public string Description { get; set; } public Teacher OnlineTeacher { get; set; } public Teacher ClassRoomTeacher { get; set; }
}

Teacher类:

 public class Teacher
{
public int TeacherId { get; set; } public string Name { get; set; } public ICollection<Course> OnlineCourses { get; set; } public ICollection<Course> ClassRoomCourses { get; set; }
}

在上面代码例子中,Course和Teacher实体有两个一对多的关系。一门课程可以被一个Online Teacher教,也可以被一个class-room Teacher教。同样一个教师可以教多个Online Course,也可以教多个class-room Course.
删掉之前生成的数据库后,我们运行一下程序:

看看生成的数据库:

看到,EF为我们在Course表中,生成了4个外键。为了解决这个问题,我们使用InverseProperty特性。修改一下Teacher类的代码:
Teacher类:

public class Teacher
{
public int TeacherId { get; set; } public string Name { get; set; } [InverseProperty("OnlineTeacher")]
public ICollection<Course> OnlineCourses { get; set; } [InverseProperty("ClassRoomTeacher")]
public ICollection<Course> ClassRoomCourses { get; set; }
}

Course类:

public class Course
{
public int CourseId { get; set; } public string CourseName { get; set; } public string Description { get; set; } public Teacher OnlineTeacher { get; set; } public Teacher ClassRoomTeacher { get; set; }
}

在上面的例子中,InverseProperty特性应用在两个集合类型的导航属性OnlineCourses和ClassRoomCourses上,然后指定它们在Course实体中的相关联的导航属性,所以现在EF就能分辨出来相应的外键。EF 6将创建两个外键:OnlineTeacher_TeacherIdClassRoomTeacher_TeacherId。对于EF Core将会创建OnlineTeacherTeacherIdClassRoomTeacherTeacherId两个外键。
然后在删除数据库,重新运行一下程序:

看一下生成的数据库:

可以看到现在生成的数据库就是正确的了。
进一步配置,你可以使用ForeignKey特性来配置外键名称:
Course类:

public class Course
{
public int CourseId { get; set; } public string CourseName { get; set; } public string Description { get; set; } [ForeignKey("OnlineTeacher")]
public int OnlineTeacherId { get; set; }
public Teacher OnlineTeacher { get; set; } [ForeignKey("ClassRoomTeacher")]
public int ClassRoomTeacherId { get; set; }
public Teacher ClassRoomTeacher { get; set; }
}

Teacher类:

public class Teacher
{
public int TeacherId { get; set; } public string Name { get; set; } [InverseProperty("OnlineTeacher")]
public ICollection<Course> OnlineCourses { get; set; } [InverseProperty("ClassRoomTeacher")]
public ICollection<Course> ClassRoomCourses { get; set; }
}

上面代码中,我们在Corse类中添加两个属性列,配置外键,现在删除数据库,重新运行一下:

看到报错了,我们修改一下Course类:
Course类最终代码:

 public class Course
{
public int CourseId { get; set; } public string CourseName { get; set; } public string Description { get; set; } [ForeignKey("OnlineTeacher")]
public int? OnlineTeacherId { get; set; }
public Teacher OnlineTeacher { get; set; } [ForeignKey("ClassRoomTeacher")]
public int? ClassRoomTeacherId { get; set; }
public Teacher ClassRoomTeacher { get; set; }
}

重新运行程序:可以看到成功了。

我们看看最终生成的数据库:

可以看到生成的外键列就是我们自己自定义的名称了。
综上所述:当两个实体间有多个关系的时候,你可以使用InverseProperty 特性和ForeignKey 特性来配置实体。

9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 微信调用itchat库 实现发消息

    import itchat,timefrom itchat.content import * itchat.auto_login(enableCmdQR=-1)while True: for i in ...

  2. Fiddler 抓包工具怎么使用?怎么在Android手机端的APP抓包

    https://blog.csdn.net/loner_fang/article/details/83309266 参考这个人的微博上有fiddler主要功能使用的步骤. 序章 Fiddler是一个蛮 ...

  3. Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

    Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...

  4. JQEUERY案例

    案例效果: 点击显示全部奶粉品牌前: 点击后: 源码: <!DOCTYPE html><html><head> <meta charset="utf ...

  5. MFC笔记7

    1.VS中显示行号 工具 -> 选项 -> 文本编辑器 -> C/C++ -> 行号 2.VS中调整字体大小 工具 -> 选项 -> 环境->字体和颜色 3. ...

  6. mysql,utf8,utf8mb4

    参考文章 https://www.cnblogs.com/beyang/p/7580814.html https://blog.csdn.net/testcs_dn/article/details/7 ...

  7. Java 运行时常量池

    运行时常量池是方法区的一部分.class中除了有类的版本,字段,方法,接口等描述信息外,还有一项信息是常量池,用于存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后存放在方法区的运行时常量池 ...

  8. 单点登录(SSO)解决方案之 CAS 入门案例

    单点登录: 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. ...

  9. 什么是PLI?

    首先,什么是PLI? 本部分设定了隐藏,您已回复过了,以下是隐藏的内容 PLI 就是product liability insurance 的简写,中文可以翻译成“产品责任险”说白了,就是你的产品如果 ...

  10. python生成器(generator)、迭代器(iterator)、可迭代对象(iterable)区别

    三者联系 迭代器(iterator)是一个更抽象的概念,任何对象,如果它的类有next方法(next python3)和__iter__方法返回自己本身,即为迭代器 通常生成器是通过调用一个或多个yi ...