在模型生成期间检测到一个或多个验证错误:\ r \ n \ r \ n

时间:2016-08-11 16:03:17

标签: c# entity-framework custom-errors

我很困惑。我正在建立一个级联下拉列表,我收到了最奇怪的错误。我一直收到以下错误(这是一个很长的错误) -

"在模型生成期间检测到一个或多个验证错误:\ r \ n \ r \ nAQB_MON.ViewModels.DeviceStatu :: EntityType' DeviceStatu'没有定义键。定义此EntityType的键。\ r \ nAQB_MON.ViewModels.SelectListItem :: EntityType' SelectListItem'没有定义键。定义此EntityType的键。\ r \ nDeviceStatus:EntityType:EntitySet' DeviceStatus'基于类型' DeviceStatu'没有定义键。\ r \ nSelectListItems:EntityType:EntitySet' SelectListItems'基于类型' SelectListItem'没有定义键。\ r \ n"}

我有一个Manufacturer表和一个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);
    }

Var制造商失败。最奇怪的是,我甚至没有,而且从未有过 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 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; }

设备型号

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

1 个答案:

答案 0 :(得分:0)

可能是错误消息的内容:EntityType&#39; DeviceStatu&#39;没有定义键。定义此EntityType的密钥。

很难说,因为您没有发布此实体的数据模型,但它可能是一个命名问题

要成为一个键,该字段必须与类如DeviceStatusId具有相同的名称,或者使用字段上方的[Key]数据注释