2016-08-11 30 views
1

我很困惑。我正在構建級聯下拉菜單,並收到最奇怪的錯誤。我一直收到以下錯誤(這是一個很長的錯誤) -模型生成期間檢測到一個或多個驗證錯誤: r n r n

「在模型生成過程中檢測到一個或多個驗證錯誤:\ r \ n \ r \ nAQB_MON.ViewModels.DeviceStatu :: EntityType'DeviceStatu' \ r \ nAQB_MON.ViewModels.SelectListItem::EntityType'SelectListItem'沒有定義密鑰。定義此EntityType的密鑰。\ r \ nDeviceStatus:EntityType:EntitySet'DeviceStatus'是基於\ r \ nSelectListItems:EntityType:EntitySet'SelectListItems'基於沒有定義鍵的類型'SelectListItem'。\ r \ n「}

我有一個製造商表和一個ManufacturerModel表。我的級聯下拉列表由用戶首先選擇製造商,然後在第二個下拉列表中提供相應的型號選項。不過,當我嘗試加載下拉菜單時,我仍然收到錯誤消息。

我創建了自己的ViewModel - ManufacturerModelContext

public class ManufacturerModelContext : DbContext 
{ 

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

,我檢索的廠商和型號與

//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); 
    } 

它未能在瓦爾製造商。最奇怪的是,我甚至不有,從來沒有也有AQB_MON.ViewModels.DeviceStatu

製造商型號

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型號

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; } 

設備型號

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; } 
+0

什麼是製造商1?顯示模型可能會有所幫助。 –

+0

我已經更新了我的問題與製造商模型和ManufactureModel模型 –

+0

您的ManufacturerModel中有一個設備引用,但我沒有看到一個DbSet。您需要添加DbSet 或將[NotMapped]添加到該導航屬性。 –

回答

0

它可能是錯誤m消息說:EntityType'DeviceStatu'沒有定義密鑰。定義此EntityType的關鍵字。

這很難說,因爲你沒有張貼這個實體的數據模型,但它可能是一個命名問題

是一個關鍵領域必須具有相同的名稱,像DeviceStatusId類或使用[鍵]數據註釋

+0

我能夠通過錯誤添加'[Key]'註釋到我的DeviceStatus模型中的鍵字段,然後我收到了另一個錯誤在我的'SelectListItem',通過在SelectListItem上添加'[NotMapped]'註釋來解決元素。但是,這一切都導致了_ CREATE FILE遇到操作系統錯誤5(訪問被拒絕。)_ _的另一個錯誤_這我至今還沒有解決。 –

+0

哦,是啊,你發佈了,有鏈接到另一個問題? – user2113566

+0

我剛剛發佈我的問題 - CREATE FILE遇到操作系統錯誤5(訪問被拒絕。) –

相關問題