C# 使用实体框架时如何填充导航属性

C# 使用实体框架时如何填充导航属性,c#,asp.net,entity-framework,linq,asp.net-web-api2,C#,Asp.net,Entity Framework,Linq,Asp.net Web Api2,我使用实体框架,数据库第一,需要一个很好的解决方案,以下问题 我有一个实体,即实体框架生成的课程 class Courses { public Courses() { this.Students=new HashSet<Student>(); } public int courseid{get;set;} public virtual ICollection<Student> Students{get;s

我使用实体框架,数据库第一,需要一个很好的解决方案,以下问题 我有一个实体,即实体框架生成的课程

class Courses
{
     public Courses()
     {
         this.Students=new HashSet<Student>();
     }
     public int courseid{get;set;}
     public virtual ICollection<Student> Students{get;set}
}

Class Student
{
     public int id{get;set;}
     public string Name{get;set;}
}
要返回Courses\u Model,我们可以在返回时创建新的Courses\u Model对象,但我不知道如何填充

public Courses_Model GetCourses(string id)
{ 
    Course= con.Courses.Include("Student").Select(g => new Courses_Model{courseid=g.courseid }).Where(g => g.REQUESTER_REFERENCE == id).First();

    return course;
}

在您的投影中,您还需要实例化Student_模型。我建议在Where之后投影,并使用lambda表达式,而不是字符串作为Include:


另一方面,像这样的库非常适合抽象此类内容。

下面是我为您创建的一个简单示例

public class STUDENTS
{
    public STUDENTS()
    {
        COURSES = new List<COURSES>();
    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ST_ROWID { get; set; }

    public int ST_NAME { get; set; }

    [ForeignKey("CR_SM_REFNO")]
    public virtual List<COURSES> COURSES { get; set; }
}

public class COURSES
{
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CR_ROWID { get; set; }

    public string CR_NAME { get; set; }

    public int CR_SM_REFNO { get; set; }

    [ForeignKey("CR_SM_REFNO")]
    public virtual STUDENTS STUDENTS { get; set; }
}
以下方法可以完成这项工作:

// gets the list of courses taken by the student id 
public List<COURSES> GetCoursesByStudent(int pST_ROWID)
    {
        using (var con = new MPContext())
        {
            return con.COURSES.Include(x=>x.STUDENTS).
                                Where(x => x.CR_SM_REFNO.Equals(pST_ROWID)).ToList();
        }
    }

    //Gets the list of students who get the course with the course id
    public List<STUDENTS> GetStudentsByCourse(int pCR_ROWID)
    {
        using (var con = new MPContext())
        {
            return con.STUDENTS.Include(x => x.COURSES).
                Where(x => x.COURSES.Any(y=>y.CR_ROWID.Equals(pCR_ROWID))).ToList();
        }
    }

谢谢vidmantas,这正是我想要的,我只是改变了我的预测,@user2889674没有问题,如果有帮助,你可以接受答案。
public Courses_Model GetCourses(string id)
{ 
    Course= con.Courses.Include(x => x.Students)
            .Where(g => g.REQUESTER_REFERENCE == id)
            .Select(
                  g => new Courses_Model
                           {
                                courseid = g.courseid,
                                Students = g.Students.Select(x => new Student_Model { id = x.id, Name = x.Name })
                           })
            .First();

    return course;
}
public class STUDENTS
{
    public STUDENTS()
    {
        COURSES = new List<COURSES>();
    }
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ST_ROWID { get; set; }

    public int ST_NAME { get; set; }

    [ForeignKey("CR_SM_REFNO")]
    public virtual List<COURSES> COURSES { get; set; }
}

public class COURSES
{
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CR_ROWID { get; set; }

    public string CR_NAME { get; set; }

    public int CR_SM_REFNO { get; set; }

    [ForeignKey("CR_SM_REFNO")]
    public virtual STUDENTS STUDENTS { get; set; }
}
// gets the list of courses taken by the student id 
public List<COURSES> GetCoursesByStudent(int pST_ROWID)
    {
        using (var con = new MPContext())
        {
            return con.COURSES.Include(x=>x.STUDENTS).
                                Where(x => x.CR_SM_REFNO.Equals(pST_ROWID)).ToList();
        }
    }

    //Gets the list of students who get the course with the course id
    public List<STUDENTS> GetStudentsByCourse(int pCR_ROWID)
    {
        using (var con = new MPContext())
        {
            return con.STUDENTS.Include(x => x.COURSES).
                Where(x => x.COURSES.Any(y=>y.CR_ROWID.Equals(pCR_ROWID))).ToList();
        }
    }