【问题标题】:how to populate navigational properties when using Entity Framework使用实体框架时如何填充导航属性
【发布时间】:2019-08-02 10:07:39
【问题描述】:

我首先使用实体​​框架,数据库,需要一个好的解决方案来解决以下问题 我有一个实体说由实体框架生成的课程

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

我在业务层中创建了与这些实体类对应的模型类

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

我想从我的 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;
}

要返回 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;
}

【问题讨论】:

    标签: c# asp.net entity-framework linq asp.net-web-api2


    【解决方案1】:

    在您的投影中,您还需要实例化 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;
    }
    

    顺便说一句,像AutoMapper 这样的库非常适合抽象这类东西。

    【讨论】:

    • 感谢 vidmantas,正是我想要的,我刚刚更改了我的投影。
    • @user2889674 没问题,如果对你有帮助,可以采纳。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      相关资源
      最近更新 更多