【问题标题】:Asp.Net MVC Reaching The Property Using Many To Many Relation With Mapping TableAsp.Net MVC 使用映射表的多对多关系达到属性
【发布时间】:2013-12-11 22:38:15
【问题描述】:

你可以看到我以前的question,它与多对多关系有关,但与自动生成的映射表有关。

我有 2 个模型,HrTraining 和 HrPerson。任何人都可以分配到一个或多个培训。你可以看到我的模型如下

public class HrTraining
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}

public class HrMapTrainingPerson
{
    public int Id { get; set; }
    public string Status { get; set; }
    public int HrTrainingId { get; set; }
    public int HrPersonId { get; set; }
    public virtual HrTraining HrTraining { get; set; }
    public virtual HrPerson HrPerson { get; set; }
}

public class HrPerson
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}

我怎样才能以有效的方式获取分配给一个人的所有训练对象。

【问题讨论】:

    标签: c# asp.net linq asp.net-mvc-4 ef-code-first


    【解决方案1】:

    所以你想找一个人,并接受分配给他的所有培训?有很多方法..但是使用你的模型,这可能是这样的

    var trPersons = dbContext.HrPerson.Find(idPerson).HrMapTrainingPerson.ToList();
    
    foreach(var trPerson in trPersons) {
    
        var training = trPerson.HrTraining;
    
        //do what you want, here you can get trPerson.HrTraining.Name for instance
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2014-08-17
      相关资源
      最近更新 更多