Ethel Patrick
Ethel Patrick

Reputation: 975

One or more validation errors were detected during model generation:\r\n\r\n

I am so confused. I am working on building a cascading dropdown and I am receiving the strangest error. I keep receiving the following error (it is a long one)-

"One or more validation errors were detected during model generation:\r\n\r\nAQB_MON.ViewModels.DeviceStatu: : EntityType 'DeviceStatu' has no key defined. Define the key for this EntityType.\r\nAQB_MON.ViewModels.SelectListItem: : EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.\r\nDeviceStatus: EntityType: EntitySet 'DeviceStatus' is based on type 'DeviceStatu' that has no keys defined.\r\nSelectListItems: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined.\r\n"}

I have a Manufacturer table and a ManufacturerModel table. My cascading dropdown consists of a user selecting a manufacturer first and then corresponding model options would be available in the second dropdown. However I keep receiving the error when trying to load the dropdown.

I created my own ViewModel - ManufacturerModelContext

public class ManufacturerModelContext : DbContext
{

    public DbSet<Manufacturer> Manufacturers { get; set; }
    public DbSet<ManufacturerModel> ManufacturerModels { get; set; }
}

and I retrieve Manufacturers and Models with

//Populate the cascading dropdowns for manufacturer and model
    ManufacturerModelContext mm = new ManufacturerModelContext();
    [HttpGet]
    public JsonResult GetManufacturers()
    {

        var manufacturer = from a in mm.Manufacturers
                           select a.Manufacturer1;

        return Json(manufacturer.ToList(), JsonRequestBehavior.AllowGet);

    }

    public JsonResult GetModelsByManufacturerID(string manufacturerId)
    {

        int Id = Convert.ToInt32(manufacturerId);

        var models = from a in mm.ManufacturerModels where a.ManufacturerID == Id select a;

        return Json(models);
    }

It fails at Var manufacturer. The strangest thing is that I don't even have and never did have a AQB_MON.ViewModels.DeviceStatu

Manufacturer Model

public partial class Manufacturer
{
    public Manufacturer()
    {
        this.ManufacturerModels = new HashSet<ManufacturerModel>();
    }

    public int ManufacturerID { get; set; }
    [Display(Name="Manufacturer")]
    public string Manufacturer1 { get; set; }
    [Display(Name="Manufacturer Description")]
    public string ManufacturerDescription { get; set; }
    public System.DateTime DATE_CREATED { get; set; }
    public string CREATED_BY { get; set; }
    public Nullable<System.DateTime> DATE_MODIFIED { get; set; }
    public string MODIFIED_BY { get; set; }

    public virtual ICollection<ManufacturerModel> ManufacturerModels { get; set; }

ManufacturerModel Model

public partial class ManufacturerModel
{
    public ManufacturerModel()
    {
        this.Devices = new HashSet<Device>();
    }

    public int ManufacturerModelID { get; set; }
    [Display(Name="Manufacturer")]
    public int ManufacturerID { get; set; }
    public string Model { get; set; }
    [Display(Name="Model Description")]
    public string ModelDescription { get; set; }
    public System.DateTime DATE_CREATED { get; set; }
    public string CREATED_BY { get; set; }
    public Nullable<System.DateTime> DATE_MODIFIED { get; set; }
    public string MODIFIED_BY { get; set; }

    public virtual ICollection<Device> Devices { get; set; }
    public virtual Manufacturer Manufacturer { get; set; }

Device Model

 public partial class Device
{
    public Device()
    {
        this.DeviceLocations = new HashSet<DeviceLocation>();
        this.DeviceLogs = new HashSet<DeviceLog>();
        this.DeviceStatus = new HashSet<DeviceStatu>();
    }


    public int DeviceID { get; set; }
    [Display(Name = "Device Type")]
    public int DeviceTypeID { get; set; }
    public int ManufacturerModelID { get; set; }
    [Display(Name="Inventory Number")]
    public string InventoryNumber { get; set; }
    [Display(Name = "Serial Number")]
    public string SerialNumber { get; set; }
    [Display(Name = "State ID")]
    public string StateID { get; set; }
    [Display(Name = "Date Received")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime? DateReceived { get; set; }
    public Nullable<decimal> Price { get; set; }
    public string Notes { get; set; }
    public System.DateTime DATE_CREATED { get; set; }
    public string CREATED_BY { get; set; }
    public Nullable<System.DateTime> DATE_MODIFIED { get; set; }
    public string MODIFIED_BY { get; set; }


    public virtual DeviceType DeviceType { get; set; }
    public virtual ManufacturerModel ManufacturerModel { get; set; }
    public virtual ICollection<DeviceLocation> DeviceLocations { get; set; }
    public virtual ICollection<DeviceLog> DeviceLogs { get; set; }
    public virtual ICollection<DeviceStatu> DeviceStatus { get; set; }

Upvotes: 1

Views: 5618

Answers (1)

user2113566
user2113566

Reputation: 85

it could be what the error message says: EntityType 'DeviceStatu' has no key defined. Define the key for this EntityType.

it's hard to tell because you didn't post this entity's data model but it could be a naming issue

to be a key the field has to have the same name as the class like DeviceStatusId or use the [Key] data annotation above the field

Upvotes: 1

Related Questions