【问题标题】:One or more validation errors were detected during model generation:\r\n\r\n在模型生成期间检测到一个或多个验证错误:\r\n\r\n
【发布时间】:2016-08-11 16:03:17
【问题描述】:

我很困惑。我正在构建一个级联下拉列表,但我收到了最奇怪的错误。我不断收到以下错误(很长)-

“在模型生成期间检测到一个或多个验证错误:\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"}

我有一个制造商表和一个制造商模型表。我的级联下拉列表包括用户首先选择制造商,然后相应的模型选项将在第二个下拉列表中可用。但是,在尝试加载下拉列表时,我一直收到错误消息。

我创建了自己的 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 型号

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

【问题讨论】:

  • 什么是Manufacturer1?展示模型可能会有所帮助。
  • 我已经用制造商模型和 ManufactureModel 模型更新了我的问题
  • 您的 ManufacturerModel 中有一个设备引用,但我没有看到 DbSet。您需要添加 DbSet 或将 [NotMapped] 添加到该导航属性。
  • 我添加了 'public DbSet Devices { get;放; }' 到 ManufacturerModel 模型,然后到 ManufacturerModelContext 模型,但我仍然收到错误。
  • 发布设备型号。它是否具有导航属性(如 DeviceStatus)?您收到一个非键错误,因此链中的每个实体都需要有一个您按约定、属性或流式 api 定义的键。所有实体也需要一个 DbSet(或 NotMapped)。

标签: c# entity-framework custom-errors


【解决方案1】:

这可能是错误消息所说的:EntityType 'DeviceStatu' has no key defined。定义此 EntityType 的键。

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

要成为键,字段必须与类同名,如 DeviceStatusId 或使用字段上方的 [Key] 数据注释

【讨论】:

  • 我能够通过将 [Key] 注释添加到我的 DeviceStatus 模型中的关键字段的错误,然后我在“SelectListItem”上收到另一个错误,该错误已通过添加 [NotMapped] 注释得到解决在 SelectListItem 元素上。然而,这一切都导致留下另一个错误_CREATE FILE遇到操作系统错误5(访问被拒绝。)_这个我至今无法解决。
  • 哦,是的,你发帖了吗,有另一个问题的链接吗?
  • 我刚刚发布了我的问题 - CREATE FILE遇到操作系统错误5(访问被拒绝。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 2011-09-26
  • 2013-02-13
相关资源
最近更新 更多