EntityType 'SelectListItem' has no key defined (Duplicated) - [Key] Is Present

丶灬走出姿态 提交于 2019-12-12 04:06:37

问题


One or more validation errors were detected during model generation:

\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.

public class Country
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Display(Name = "Country Name")]
    public string Name { get; set; }
}


public List<SelectListItem> country = new List<SelectListItem>();
        public List<SelectListItem> Country
        {
            get
            {

                country.Clear();
                using (UsersContext DB = new UsersContext())
                {
                    var cat = DB.Countries;
                    foreach (var q in cat)
                    {
                        country.Add(new SelectListItem() { Text = q.Name, Value = q.Id.ToString() });
                    }
                    return country;
                }
            }
        }

But I have already declared [Key]. What I have to do else?

来源:https://stackoverflow.com/questions/30773649/entitytype-selectlistitem-has-no-key-defined-duplicated-key-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!