【问题标题】:Mapping Many to many relationship with single model使用单个模型映射多对多关系
【发布时间】:2016-02-03 05:30:23
【问题描述】:

我的模特

public class Student
    {
        public int id{ get; set; }

        public string Name{ get; set; }

        public string Qualification{ get; set; }

   }

在我的项目中,我正在尝试创建学生的合作伙伴,一个学生可以与任意数量的学生合作。我知道我可以通过创建另一个这样的模型来实现这一目标

 public class Partners
    {

        public string student1ID{ get; set; }

        public string student2ID{ get; set; }

   }

并插入值,但只是想知道是否有任何其他方法可以使用虚拟列表等来实现这一点。

【问题讨论】:

    标签: asp.net-mvc model mapping many-to-many


    【解决方案1】:

    要建立每个学生都有一个或多个合作伙伴的关系,您可以这样做:

    public class Student
    {
        public int Id{ get; set; }
        public string Name{ get; set; }
        public string Qualification{ get; set; }
        public virtual ICollection<Partner> Partners{ get; set; }     
    }
    
    public class Partner
    {
        public int Id{ get; set; }
        public virtual Student Student{ get; set; }
    }
    

    【讨论】:

    • 实际上我正在寻找不需要创建另一个模型的解决方案。
    • @aradhanalohan,您无需创建另一个模型 - 您可以修复当前模型中的错误
    • 是的,但这是一个解决方案,我想知道 mvc 是否允许另一种解决方案,我不必只使用学生来创建合作伙伴类,这可能吗?
    • 因为我正在为类本身创建多对多关系,所以有没有其他方法可以在同一个类中配置它,希望你得到我的问题
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2014-08-17
    • 2020-09-27
    • 2011-07-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多