C# 将非关系属性添加到EF实体(联接操作)

C# 将非关系属性添加到EF实体(联接操作),c#,mysql,entity-framework,jqgrid,C#,Mysql,Entity Framework,Jqgrid,我有一个模型,如下所述: public class Student { public int Id { get; set; } public string Level { get; set; } public string Subject { get; set; } public string School { get; set; } public int CityId { get; set; } //from City } public class C

我有一个模型,如下所述:

public class Student
{
    public int Id { get; set; }
    public string Level { get; set; }
    public string Subject { get; set; }
    public string School { get; set; }
    public int CityId { get; set; } //from City
}

public class City
{
    public int Id { get; set; }
    public string City { get; set; }
    public int StateId { get; set; } //from State
}

public class State
{
    public int Id { get; set; }
    public string State { get; set; }
    public int CountryId { get; set; } //from Country
}

public class Country
{
    public int Id { get; set; }
    public string Country { get; set; }
}
我想查询学生实体,需要在学生模型中包含国家(仅国家)。 数据库结构如上所述,我无法更改DB表结构


但我可以编辑上面的EF实体。最终,我需要将国家作为学生实体的学生信息,以便我可以绘制jqGrid并根据国家对其进行排序。

您可以将其投影到匿名对象或viewmodel中:

var studentWithCountry = context.Student.Where(Id = myVar.Id)
   .Select(s => new { Level = s.Level,
                      Subject = s.Subject,
                      ...
                      Country = s.City.State.Country.Country });

如果无法修改实体类以添加相应的导航属性,可以尝试以下操作:

var query = from student in context.Students
            join city in context.Cities on student.CityId equals city.Id
            join state in context.States on city.StateId equals state.Id
            join country in context.Countries on state.CountryId equals country.Id
            select new {
                         StudentId=student.Id,
                         Level=student.Level,
                         ...
                         Country=country.Country
                       };
或者可能:

           select new {Student=student, Country=country.Country};
如果您的表是相关的,我建议您将导航属性包括到模型中:

public class Student
{
    public int Id { get; set; }
    //...
    public int CityId { get; set; } //from City

    public virtual City City{get;set;}
}

public class City
{
    public int Id { get; set; }
    //...
    public int StateId { get; set; } //from State

     public virtual City City{get;set;}
     public virtual ICollection<Student> Students{get;set;}
}

public class State
{
    public int Id { get; set; }
    //...
    public int CountryId { get; set; } //from Country
    public virtual Country Country {get;set;}
    public virtual ICollection<City> Cities{get;set;}
}

public class Country
{
    public int Id { get; set; }
    public string Country { get; set; }

    public virtual ICollection<State> States{get;set;}
}

谢谢顺便说一句,这个国家与学生排行榜毫无关系。结果会是学生模型吗?我需要的结果是一个学生模型,这样我就可以在jqGrid上轻松地进行排序。我可以修改学生模型,但不能修改表格。(CodeFirst方法)这是一个匿名类型,所有属性都有
学生
+国家名谢谢!我需要类型为Student模型,是否有可能为County提供任何虚拟属性,以便模型如下所示:公共类Student{public int Id{get;set;}公共字符串级别{get;set;}公共字符串主题{get;set;}公共字符串学校{get;set;}公共int CityId{get;set;}//来自城市公共虚拟国家{get;set;}}也许你可以使用一个属性来保存国家名称,但我建议你将查询结果保存在一个对象中。谢谢!我需要将结果作为学生模型。我可以修改学生模型以采用国家关系,而不是DB表。它是代码优先EF方法,因此更改不需要影响DBRight,要使其生效,你需要添加Octaviccl建议的导航属性。这些属性不应更改数据库,因为您符合entity+Id外键约定。除非数据库中未定义关系。
var countryName=student.City.State.Country.Country;