我有一個模型類EntityType'SelectListItem'沒有定義鍵。定義鍵此的EntityType
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public ICollection<SelectListItem> CourseList { get; set; }
}
和
public class StudentContext : DbContext
{
public DbSet<Student> Students { get; set; }
}
和我嘗試TI使用它作爲
List<Student> sList = db.Students.ToList();
和我收到以下錯誤
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined.
請建議我在哪裏做錯了。
我用selectitemlist,因爲當我編輯的學生,我需要一個列表框方含所有課程,我已經預先選擇哪個學生的課程已經選擇!我只是試圖通過EF檢索現在 ,你可以讓我知道你提到的上述代碼的哪個表結構,因爲我很困惑我將如何將CourseList映射到學生。 @dotnetom – KmrRaz 2014-11-23 16:46:25
@KmrRaz爲了顯示數據庫中的數據,你應該創建一個特定的對象'StudentViewModel'。這是一個僅用於顯示目的的特定對象。在這裏有與SelectListItems,您可以顯示在視圖中的屬性。在你的控制器方法中加載'Student'類型的數據並將其映射到'StudentViewModel' – dotnetom 2014-11-23 16:53:09
你可以讓我知道StudentViewModel的結構,以便我試試!我非常感謝你能幫助我! – KmrRaz 2014-11-23 16:58:36