简体   繁体   English

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

[英]how to populate navigational properties when using Entity Framework

I am using Entity Framework, Database first and need a good solution to the below problem I have an Entity say Courses generated by Entity Framework 我先使用实体​​框架,数据库,然后需要一个很好的解决方案来解决以下问题我有一个实体说实体框架生成的课程

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;}
}

i created Model classes in my business layer which corresponds to these Entity Classes 我在业务层中创建了与这些实体类相对应的模型类

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

Class Student_Model
{
     public int id{get;set;}
     public string Name{get;set;}
}

i want to return the model class(Courses_Model) from my web api method which should include navigation property students instead of 我想从我的Web api方法返回模型类(Courses_Model),该类应该包括导航属性的学生而不是

public Courses GetCourses(string id)
{ 
    var course= (from g in con.Courses.Include("Student") where g.REQUESTER_REFERENCE == id select g).First();

    return course;
}

To return Courses_Model we can create new Courses_Model object while returning like, but i am not sure how to populate 要返回Courses_Model,我们可以在返回时创建新的Courses_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;
}

In your projection you need to instantiate Student_Model as well (also I would suggest projecting after Where and using lambda expressions rather than strings for Include ): 在您的投影中,您还需要实例化Student_Model (同样,我建议在Where之后投影,并使用lambda表达式而不是Include字符串):

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;
}

On a side note, libraries like AutoMapper are great for abstracting this kind of thing. 附带一提,像AutoMapper这样的库非常适合抽象这种事情。

below is a simple sample I created for you 以下是我为您创建的一个简单示例

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; }
}

and the following methods do the job: 并通过以下方法完成工作:

// 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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架自定义导航属性 - Entity Framework Custom Navigational Properties 使用实体框架和 OData 查询的导航 DTO 属性 - Navigational DTO properties using Entity Framework with OData Queries 实体框架4每个层次结构表 - 如何定义儿童的导航属性? - Entity Framework 4 Table Per Hierarchy - How To Define Navigational Properties On Children? Entity Framework 6代码优先中的多维导航属性 - Multidimensional navigational properties in Entity Framework 6 Code First 实体框架中的Restsharp和关系/导航属性 - Restsharp and relations / navigational properties in Entity Framework 使用Include(…)返回具有导航属性的实体时失去连接 - Losing connection when returning entity with navigational properties using Include(…) 实体框架:导航属性-代码优先 - Entity Framework: Navigational properties - Code First 阻止实体框架为导航属性插入值 - Prevent Entity Framework to Insert Values for Navigational Properties 实体框架3-链式导航属性中的过滤器元素 - Entity Framework 3 - Filter Elements in Chained Navigational Properties 实体框架4抽象模型 - 如何以编程方式加载导航属性? - Entity Framework 4 Abstract Model - How to Programatically Eager-Load Navigational Properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM