设为首页 加入收藏

TOP

9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】(一)
2019-09-17 19:08:39 】 浏览:85
Tags:9.7 翻译 系列 数据 注解 特性 --InverseProperty Code-First

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

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

当两个实体之间不止一种关系的时候,可以使用InverseProperty特性,为了理解InverseProperty特性我们来做一个例子:
1.创建一个控制台应用程序,安装好EF:
enter description here
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" pro
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.NET CAD二次开发学习第一天 下一篇使用 VS Code 开发和调试 .NET Co..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目