EntityType'SelectListItem'没有定义键.定义此EntityType的键 [英] EntityType 'SelectListItem' has no key defined. Define the key for this EntityType

查看:113
本文介绍了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; }
}

我尝试将其用作

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.

请建议我在哪里做错了.

Please suggest where i am doing wrong.

推荐答案

您不应尝试将SelectListItem存储在数据库中,因为这是MVC特定的概念.而是创建一个自定义实体类并使用它;

You should not be attempting to store SelectListItem in the database, as this is MVC specific concept. Instead create a custom entity class and use it instead;

public class Course
{
    public int CourseId { get; set; }
    public string CourseTitle  { get; set; }
}

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public ICollection<Course> CourseList { get; set; }
}

public class StudentContext : DbContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Course> Courses { get; set; }
}

这篇关于EntityType'SelectListItem'没有定义键.定义此EntityType的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆