EntityType & # 39; SelectListItem& # 39;没有定义键

本文关键字:定义 EntityType SelectListItem | 更新日期: 2023-09-27 18:02:17

在数据库中的空表上,我在返回视图(context.Customer.ToList())上得到索引home控制器上的这个错误;

System.Data.Edm.EdmEntityType: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �SelectListItems� is based on type �SelectListItem� that has no keys defined.

我已经在我的模型中添加了[key]属性到id(带有getter和setter), edmx文件在我的项目中包含表customer,其中id为主键,与我的模型相同。

我还需要做什么????

EntityType & # 39; SelectListItem& # 39;没有定义键

请看看这个链接,它向您展示了如何在没有从表中推断出键的情况下添加实体键

  1. 取消.edmx文件中SSDL部分的EntityType元素的注释。
  2. 通过向EntityType元素添加适当的key元素来定义实体类型的键。
  3. 在SSDL部分中,添加EntitySet元素,指定添加的实体类型映射到的表。
  4. 在CSDL部分,添加一个EntityType元素,与添加的SSDL实体类型相对应。
  5. 在C-S(概念到存储)映射部分,添加一个EntitySetMapping元素,指定添加的SSDL和CSDL实体类型之间的映射。

以上要点摘自链接