脚手架新视图时的验证错误

时间:2017-08-07 12:30:07

标签: c# asp.net-mvc asp.net-mvc-5

编辑2:

我通过从DbSets中删除我的一个POCO类来修复此问题。这是消除过程的开始,看看它们中是否有任何一个是问题的原因。

罪犯是这样的:

public class Error
{
    public int Id { get; set; }
    public string Component { get; set; }
    public string Action { get; set; }
    public DateTime Date { get; set; }
    public Exception Exception { get; set; }
}

有什么想法为什么会这么做?

编辑1:

当我尝试添加数据迁移时也会发生错误。从包管理器控制台:

One or more validation errors were detected during model generation:

SuppliersMVC.Models.Exception: : EntityType 'Exception' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.IdentityReference: : EntityType 'IdentityReference' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.AssemblyName: : EntityType 'AssemblyName' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.CultureInfo: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.DateTimeFormatInfo: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
Exceptions: EntityType: EntitySet 'Exceptions' is based on type 'Exception' that has no keys defined.
IdentityReferences: EntityType: EntitySet 'IdentityReferences' is based on type 'IdentityReference' that has no keys defined.
AssemblyNames: EntityType: EntitySet 'AssemblyNames' is based on type 'AssemblyName' that has no keys defined.
CultureInfoes: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
DateTimeFormatInfoes: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.

这是我的DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public virtual DbSet<Error> Errors { get; set; }
    public virtual DbSet<PaymentTerm> PaymentTerms { get; set; }
    public virtual DbSet<Supplier> Suppliers { get; set; }
    public virtual DbSet<SupplierDocument> SupplierDocuments { get; set; }
    public virtual DbSet<SupplierService> SupplierServices { get; set; }
    public virtual DbSet<Voucher> Vouchers { get; set; }

    public ApplicationDbContext()
        : base("DevConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

错误中引用的类(SuppliersMVC.Models.ExceptionSuppliersMVC.Models.IdentityReference等)也不会出现在我的对象浏览器中。

值得注意的是,该项目没有任何错误。当我尝试添加迁移时,输出窗口不显示任何内容。

问题

我试图构建我的视图,以便我可以进入并对生成的代码进行必要的更改,而不必自己重写所有标记(这样做是为了节省时间)。

我尝试此操作时会出现一些验证错误,如下图所示,但出于质量目的,我也会在此处引用它们。

这是我的ViewModel(我试图搭建)的样子:

public class AddEditSupplierViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ServiceId { get; set; }
    public IEnumerable<SelectListItem> Services { get; set; }
    public int SelectedRating { get; set; }
    public IEnumerable<SelectListItem> RatingList { get; set; }
    public string Address { get; set; }
    public string EmailAddress { get; set; }
    public string Telephone { get; set; }
    public double Commission { get; set; }
    public int PaymentTermId { get; set; }
    public IEnumerable<SelectListItem> PaymentTerms { get; set; }
    public bool Preferred { get; set; }
    public DateTime BEEExpiry { get; set; }
    public string SHEQ { get; set; }

    public AddEditSupplierViewModel()
    {
        var List = new List<int> { 1, 2, 3, 4, 5 };

        RatingList = List.Select(x => new SelectListItem
        {
            Value = x.ToString(),
            Text = x.ToString()
        });
    }
}

看起来错误继续超出错误框的界限,所以我不认为这就是全部,但它看起来并不像我的viewmodel。

我把它作为一个简单的MVC项目开始。我没有做任何特别的事情;刚刚将我的模型添加到ApplicationDbContext中,这个模型按照ASP.NET身份构建,因为我已经在几个没有这些问题的项目中完成了。

任何想法是怎么回事?我怎样才能摆脱这些并支撑我的观点?

  

运行所选代码生成器时出错:
  &#39;无法检索到的元数据   &#39; SuppliersMVC.Models.AddEditSupplierViewModel&#39 ;.在模型生成期间检测到一个或多个验证错误:

     

例外:: EntityType&#39;异常&#39;没有定义键。为此定义一个键   实体类型。

     

IdentityReference :: EntityType&#39; IdentityReference&#39;没有定义键。为此实体类型定义密钥。

     

AssemblyName :: EntityType&#39; AssemblyName&#39;没有定义键。定义此EntityType的键

     

DateTimeFormatInto :: EntityType&#39; DateTimeFormatInfo&#39;没有定义键。定义此EntityType的键

     

例外:EntityType:EntitySet&#39;例外情况基于类型&#39;异常&#39;没有定义键。

     

IdentityReferences:EntityType:EntitySet&#39; IdentityReferences&#39;基于类型&#39; IdentityReference,没有定义键。

     

AssemblyNames:EntityType:EntitySet&#39; AssemblyNames&#39;基于类型&#39; AssemblyNames&#39;没有定义键。

     

CultureInfoes:EntityType:EntitySet&#39; CultureInfoes&#39;是基于类型&#39; CultureInfo&#39;没有定义键。

     

DateTimeFormatInfoes:EntityType:EntitySet&#39; DateTimeFormatInfoes&#39;基于类型&#39; DateTimeFOrmatInfo&#39;没有定义键。

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试将课程更改为:

 public class AddEditSupplierViewModel
 {
    [Key] // add key to Primary Key
    public int Id {get; set; }

    public string Name { get; set; }
    public int ServiceId { get; set; }
    public IEnumerable<SelectListItem> Services { get; set; }
    public int SelectedRating { get; set; }
    public IEnumerable<SelectListItem> RatingList { get; set; }
    public string Address { get; set; }
    public string EmailAddress { get; set; }
    public string Telephone { get; set; }
    public double Commission { get; set; }
    public int PaymentTermId { get; set; }
    public IEnumerable<SelectListItem> PaymentTerms { get; set; }
    public bool Preferred { get; set; }
    public DateTime BEEExpiry { get; set; }
    public string SHEQ { get; set; }

    public AddEditSupplierViewModel()
    {
        var List = new List<int> { 1, 2, 3, 4, 5 };

        RatingList = List.Select(x => new SelectListItem
        {
            Value = x.ToString(),
            Text = x.ToString()
        });
    }
}

注意:如果该属性的名称不是Id,则需要为其添加[Key]属性。