Asp.net mvc 使用ASP.NET标识的WinForms应用程序中的ModelValidationException:EntityType未定义键
我正在使用ASP.NET MVC5和ASP.NET身份验证来开发一个web应用程序。我的数据库首先是用EF代码建立的。我使用以下属性扩展了ApplicationUser:Asp.net mvc 使用ASP.NET标识的WinForms应用程序中的ModelValidationException:EntityType未定义键,asp.net-mvc,winforms,asp.net-mvc-5,ef-code-first,asp.net-identity,Asp.net Mvc,Winforms,Asp.net Mvc 5,Ef Code First,Asp.net Identity,我正在使用ASP.NET MVC5和ASP.NET身份验证来开发一个web应用程序。我的数据库首先是用EF代码建立的。我使用以下属性扩展了ApplicationUser: public class ApplicationUser : IdentityUser { [StringLength(80)] [Display(Name = "FullName", ResourceType = typeof(Resources.Resources))] public string
public class ApplicationUser : IdentityUser
{
[StringLength(80)]
[Display(Name = "FullName", ResourceType = typeof(Resources.Resources))]
public string FullName { get; set; }
// More properties
public virtual ICollection<Installation> Installations { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
公共类应用程序用户:IdentityUser
{
[条次建议修正案(80)]
[显示(Name=“FullName”,ResourceType=typeof(Resources.Resources))]
公共字符串全名{get;set;}
//更多属性
公共虚拟ICollection安装{get;set;}
公共异步任务GenerateUserIdentityAsync(UserManager我使用以下两行代码使用用户和密码登录:
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = await userManager.FindAsync(this.tbUser.Text, this.tbPassword.Text);
var userManager=new userManager以获取更多信息,但这些是不同的异常,因此它永远不会进入catch块
最奇怪的是,在它工作正常之前,我不知道我做了什么,现在我得到了这个错误。我已经解决了重写ApplicationDbContext类的ModelCreating方法并指定键的问题:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Installation>().HasKey(c => c.Imei);
base.OnModelCreating(modelBuilder);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasKey(c=>c.Imei);
基于模型创建(modelBuilder);
}
System.Data.Entity.ModelConfiguration.ModelValidationException was unhandled by user code
HResult=-2146233088
Message=One or more validation errors were detected during model generation:
MyProjectWeb.Models.Installation: : EntityType 'Installation' has no key defined. Define the key for this EntityType.
Installations: EntityType: EntitySet 'Installations' is based on type 'Installation' that has no keys defined.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.QueryableExtensions.Include[T](IQueryable`1 source, String path)
at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable`1 source, Expression`1 path)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.GetUserAggregateAsync(Expression`1 filter)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindByNameAsync(String userName)
at Microsoft.AspNet.Identity.UserManager`2.FindByNameAsync(String userName)
at Microsoft.AspNet.Identity.UserManager`2.<FindAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyProjectForm.FormConectar.<Login>d__2.MoveNext() in f:\...\Solution\MyProjectForm\FormConectar.cs:line 158
InnerException:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Installation>().HasKey(c => c.Imei);
base.OnModelCreating(modelBuilder);
}