简体   繁体   English

如何在实体框架中使用linq返回此结果(2个外键映射表)

[英]How to return this result using linq in entity framework( 2 foreign key mapping table)

I have a table that contains 2 foreign key that reference separately to 2 different table. 我有一个表包含2个外键,分别引用到2个不同的表。 I would like to return the result of all person that has course of "Science". 我想返回所有有“科学”课程的人的成绩。

How to retrieve the record back using LINQ? 如何使用LINQ检索记录? This is what i gotten so far: 这是我到目前为止得到的:

       return 
        _ctx.Person
            .Include(u => u.Course
            .Where(ug=>ug.CourseName== "Science"));

This is not working as it shows the error. 这不起作用,因为它显示错误。

The Include path expression must refer to a navigation property defined on the type Include路径表达式必须引用在类型上定义的导航属性

public class Course
{
    public int CourseID {get; set;}
    public string CourseName {get; set;}

    public virtual ICollection<Person> Persons { get; set; }
}

public class Person
{
   public int PersonID {get; set;}
   public virtual ICollection<Course> Courses { get; set; }
}

This is the mapping table. 这是映射表。 Only contains 2 foreign key from 2 different table. 仅包含2个不同表中的2个外键。 I could not use this table inside the solution.As the code first won't generate this table as it doesn't contain it's own PK. 我无法在解决方案中使用此表。因为代码首先不会生成此表,因为它不包含它自己的PK。

//This is not shown in the EntityFramework when generating Code First.
    public class PersonCouseMap 
    {
      public int PersonID {get; set;}
      public int CourseID {get; set;}
    }

Update : this works after I switched the entity. 更新:这在切换实体后起作用。

return _ctx.Course
       .Include(u=>u.Person)
       .Where(ug=>ug.CourseName == "Sciene");

Anyone can explain why it won't work the another way round. 任何人都可以解释为什么它不会以另一种方式运作。 I need to display a List of Person who have course of "Science", not Course Science that has a list of user. 我需要显示具有“科学”课程的人员列表,而不是具有用户列表的课程科学。

The original query does not work because you've pushed the Where predicate inside the Include expression, which is not supported as indicated by the exception message. 原始查询不起作用,因为您已在Include表达式中推送Where谓词,而异常消息指示不支持该谓词。

The Include method is EF specific extension method used to eager load related data. Include方法是EF特定的扩展方法,用于预先加载相关数据。 It has nothing to do with the query filtering. 它与查询过滤无关。

To apply the desired filter person that has course of "Science" you need Any based predicate since the Person.Courses is a collection: 要应用具有“科学”课程的所需过滤器人,您需要Any基于谓词,因为Person.Courses是一个集合:

return _ctx.Person
    .Where(p => p.Courses.Any(c => c.CourseName == "Science"));

To include the related data in the result, combine it with Include call(s): 要在结果中包含相关数据,请将其与Include call(s)组合:

return _ctx.Person
    .Include(p => p.Courses)
    .Where(p => p.Courses.Any(c => c.CourseName == "Science"));

It looks like there is no relations between these two entites, you can establish a relationship by making the following changes to your code: 看起来这两个实体之间没有任何关系,您可以通过对代码进行以下更改来建立关系:

Here I am assuming that you want to establish Many-to-Many relationship between these two tables by having a third entity PersonCourseMap 在这里,我假设你想通过拥有第三个实体PersonCourseMap来建立这两个表之间的多对多关系

public class Course
{
    public int CourseID {get; set;}
    public string CourseName {get; set;}

   public virtual ICollection<CoursePersons> Courses { get; set; }
}

public class Person
{
   public int PersonID {get; set;}
   public virtual ICollection<PersonCourse> Courses { get; set; }
}

public class PersonCourseMap 
{
  public int PersonID {get; set;}
  public int CourseID {get; set;}

  public virtual ICollection<Person> Persons { get; set; }
  public virtual ICollection<Course> Courses { get; set; }
}

After making above changes you can simply navigate through properties. 完成上述更改后,您只需浏览属性即可。

Include Foreign Key Mapping 包括外键映射

public class Course
{
    public int CourseID {get; set;}
    public string CourseName {get; set;}
    public virtual ICollection<Person> Person {get; set}
}

public class Person
{
   public int PersonID {get; set;}
   public virtual ICollection<Course> Course {get; set;}
}

using System.ComponentModel.DataAnnotation.Schema;
public class PersonCouseMap 
{
   [ForeignKey("Person")]
   public int PersonID {get; set;}
   [ForeignKey("Course")]
   public int CourseID {get; set;}

   public virtual ICollection<Person> Person {get; set;}
   public virtual ICollection<Course> Course {get; set;}
}

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

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