【发布时间】:2014-11-19 21:44:36
【问题描述】:
我使用实体框架和 MVC。我需要按地址 ID 排序的地址列表。但是当我为排序封装属性时,实体框架不会填充地址列表。 我该怎么办?
public class Student
{
public int StudentId { get; set; }
public string Name{ get; set; }
public string Surname{ get; set; }
//i need this address list order by address id???
public virtual List<StudentAddress> Address { get; set; }
}
public class StudentAddress
{
[Key]
public int AddressId{ get; set; }
public string Address { get; set; }
public string City { get; set; }
}
【问题讨论】:
-
为什么要关心类中列表的顺序?如果您想以特定顺序显示它,请在 UI 中对其进行排序。
-
因为我经常使用它。
-
然后在需要时订购。另外,我怀疑按 ID 排序并不总是一个好主意,如果学生发生变化并且 ID 2 成为主要地址怎么办?
-
你应该调用 Include 例如: db.Students.Include("Address").ToList();或 db.Students.Include(t=>t.Address).ToList();添加后使用 System.Data.Entity;
标签: c# entity-framework