每天学一点,每天积累一点,进步就不止一点点!PS:好记性不如烂笔头,学会总结,学会思考~~~ ----要飞翔,必须靠自己!

灰太狼的梦想

好记性不如烂笔头,学会总结,学会思考~~~

9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 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" 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.运行程序:
enter description here
可以看到,根据默认的约定,生成的数据库如下:
enter description here

现在假设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.
删掉之前生成的数据库后,我们运行一下程序:
enter description here
看看生成的数据库:
enter description here
看到,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两个外键。
然后在删除数据库,重新运行一下程序:
enter description here

看一下生成的数据库:
enter description here

可以看到现在生成的数据库就是正确的了。
进一步配置,你可以使用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类中添加两个属性列,配置外键,现在删除数据库,重新运行一下:
enter description here
看到报错了,我们修改一下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; }
    }

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

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

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

posted @ 2019-04-09 15:33 灰太狼的梦想 阅读(...) 评论(...) 编辑 收藏