【发布时间】:2017-08-07 21:36:23
【问题描述】:
编辑 2:
我通过从 DbSet 中删除我的一个 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.Exception、SuppliersMVC.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()
});
}
}
看起来错误继续超出了错误框的范围,所以我认为这不是全部,但这看起来与我的视图模型无关。
我将其作为一个简单的 MVC 项目开始。我没有做任何特别的事情。刚刚将我的模型添加到 ApplicationDbContext 中,它按照 ASP.NET 身份构建,就像我在几个没有这些问题的项目中所做的那样。
有什么想法吗?我怎样才能摆脱这些并搭建我的视图?
运行所选代码生成器时出错:
'无法检索
的元数据 '供应商MVC.Models.AddEditSupplierViewModel'。在模型生成期间检测到一个或多个验证错误:Exception: : EntityType 'Exception' 没有定义键。为此定义一个键 实体类型。
IdentityReference: : EntityType 'IdentityReference' 没有定义键。为此实体类型定义一个键。
AssemblyName: : EntityType 'AssemblyName' 没有定义键。定义此 EntityType 的键
DateTimeFormatInto: : EntityType 'DateTimeFormatInfo' 没有定义键。定义此 EntityType 的键
异常:EntityType:EntitySet 'Exceptions 基于没有定义键的类型'Exception'。
IdentityReferences: EntityType: EntitySet 'IdentityReferences' 基于没有定义键的类型'IdentityReference。
AssemblyNames: EntityType: EntitySet 'AssemblyNames' 基于没有定义键的类型'AssemblyNames'。
CultureInfoes: EntityType: EntitySet 'CultureInfoes' 基于没有定义键的类型'CultureInfo'。
DateTimeFormatInfoes: EntityType: EntitySet 'DateTimeFormatInfoes' 基于没有定义键的类型 'DateTimeFOrmatInfo'。
【问题讨论】:
-
您的视图模型需要位于单独的文件夹中,因此它们不会与 EF 关联。
-
我认为通过不将
public DbSet<viewmodel> viewmodels { get; set; }添加到 DbContext 来简单地将它们从数据模型中排除就足以消除与 EF 的关联。不是这样吗? -
无论如何,我已将 ViewModel 移至其自己的命名空间
-
没有解决问题
-
不行,你需要把它们放到一个单独的文件夹中(比如
VIewModels)
标签: c# asp.net-mvc asp.net-mvc-5