简体   繁体   中英

How do I load related entities from a database view using entity framework?

I have been struggling with a "one to many" relationship regarding a database view.

Im using entity framework and I have this "simplified" model:

A "Course" entity. A "Student" entity.

A Course can have many Students.

The following code works fine:

public class Course
{
    public int Id { get; set; }
    public virtual List<Student> Students { get; set; }
}
public class Sudent
{
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}


private List<Course> GetCourses()
{
    List<Course> courses;
    using (var db = context ?? new MyContext())
    {
        courses = (from course in db.Courses select course)
                    .Include(l => l.Students)
                    .ToList();
    }
}

Here is my MyContext:

public class MyContext : DbContext
{
    protected MyContext()
    {}
    public virtual DbSet<Course> Courses { get; set; }
    public virtual DbSet<OpenCourse> OpenCourses { get; set; }
}

I have a view "OpenCourses" that gets all courses that are open.

public class OpenCourse
{
    public int Id { get; set; }
    public virtual List<Student> Students { get; set; }
}

private List<OpenCourse> GetOpenCourses()
{
    List<Course> courses;
    using (var db = context ?? new MyContext())
    {
        courses = (from course in db.OpenCourses select course)
                    .Include(l => l.Students)
                    .ToList();
    }
}

When I run it I get the following inner-exception: "Invalid column name 'OpenCourse_Id'."

I get that I somehow have to associate the OpenCourse-view with many Students. But how?

Prefereably I would like to do this in my method and my lambda expression.

You don't have a relation property, from Student to OpenCourse.

You can add it like this.

public class Student
{
    public int CourseId { get; set; }
    public int OpenCourseId { get; set; }
    public virtual Course Course { get; set; }
}

But at this time, I think you need to review your domain design.

Also check this for n to m relations. https://stackoverflow.com/a/8927380/465536

You are going to need to override the OnModelCreating method of the MyContext object. Entity Framework has some other ways to define relationships on the model itself, but this is unlikely to work with your current setup (multiple entities having a collection of students).

I am assuming that your OpenCourses view is actually a wrapper around your Courses table. This means I am assuming that Student.CourseId is also your foreign key to OpenCourses.

With that in mind, this is how OnModelCreating might look

public class MyContext : DbContext
{
    protected MyContext()
    {}
    public virtual DbSet<Course> Courses { get; set; }
    public virtual DbSet<OpenCourse> OpenCourses { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<OpenCourse>()
        .HasMany<Student>(p => p.Students)
        .WithRequired()
        .HasForeignKey(p => p.CourseId);

    base.OnModelCreating(modelBuilder);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM