【问题标题】:how make relationship between two table asp.core code first如何首先在两个表 asp.core 代码之间建立关系
【发布时间】:2019-09-17 07:57:19
【问题描述】:

我有两张桌子,Person 和 Courses 这是表人:

 public int ID { get; set; }
 public int Name { get; set; }
 public int CourseID1 { get; set; }
 public int CourseID2 { get; set; }

表课程:

 public int CourseID { get; set; }
 public int Name { get; set; }

每个人都有 2 门课程。 我确实知道如何首先在 asp.core 代码中为他们建立关系

【问题讨论】:

    标签: c# entity-framework asp.net-core code-first


    【解决方案1】:
        public int ID { get; set; }
        public int Name { get; set; }
        public int Course1CourseID { get; set; }
        public Course Course1 { get; set; }
        public int Course2CourseID { get; set; }
        public Course Course2 { get; set; }
    

    编辑:他这样问“人有 2 门课程”

    id 名称 Course1 Course2 1 约翰 php asp 2 迈克 c# 核心

    以上模型将为您提供。

    【讨论】:

      【解决方案2】:

      您的要求

      每个人都有两门课程

      所以我会这样设计你的实体。 Person 类将有 ICollection 课程。 ICollection 表示您至少有一个或多个与 Course 类的关系数据(意味着 1 人可以拥有超过 1 门课程)

      人物类

          public int ID { get; set; }
          public int Name { get; set; }
          public ICollection<Courses> Courses { get; set; }
      

      Courses 类必须引用回 Person 类。 1门课程属于一个人

          public int CourseID { get; set; }
          public string Name { get; set; }
          public Person Person { get;set; } 
      

      如果您需要任何帮助,请告诉我

      【讨论】:

      • 你不认为延迟加载集合更好吗?
      • @ShivamSood 如果您认为有更好的解决方案,请添加您的 anwser,以便我向您学习。
      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2020-01-17
      相关资源
      最近更新 更多