在模型生成过程中检测到一个或多个验证错误:

本文关键字:一个 验证 错误 模型 过程中 检测 | 更新日期: 2023-09-27 18:02:05

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

"在模型生成过程中检测到一个或多个验证错误:'r'n'r'nAQB_MON.ViewModels. "DeviceStatu:: EntityType 'DeviceStatu'没有定义键。定义这个EntityType的键。SelectListItem:: EntityType 'SelectListItem'没有定义键。定义这个EntityType的键。'r'nDeviceStatus: EntityType: EntitySet 'DeviceStatus'基于没有定义键的类型'DeviceStatus'。'r'nSelectListItems: EntityType: EntitySet 'SelectListItems'基于没有定义键的类型'SelectListItems'。' 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 manufacturer处失败。最奇怪的是,我甚至没有也从未有过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; }

在模型生成过程中检测到一个或多个验证错误:

可能是错误消息所说的:EntityType 'DeviceStatu'没有定义键。为EntityType定义键

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

作为键,字段必须与类(如DeviceStatusId)具有相同的名称,或者在字段

上使用[key]数据注释。