我无法登录我的应用程序,这是我在 AccountController 中使用的代码:
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var allowed = Membership.ValidateUser(model.Username, model.Password);
if (allowed)
{
var result = SignInStatus.Failure;
var user = UserManager.FindByNameAsync(model.Username).Result;
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
result = SignInStatus.Success;
}
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Vous n'êtes pas autorisé à utiliser l'application");
return View(model);
}
}
else
{
ModelState.AddModelError("", "Tentative de connexion non valide (Nom d'utilisateur ou mot de passe incorrect)");
return View(model);
}
}
->我在此行有一个错误:
var user = UserManager.FindByNameAsync(model.Username).Result;
错误说明:
[InvalidOperationException: The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).]
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +5240430
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +71
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +484
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +174
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +269
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +77
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +53
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken) +193
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate) +157
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +502
我不知道为什么会出现此错误,因为我没有更改AccountController代码。 如果有人有任何想法
答案 0 :(得分:0)
查看程序包管理器控制台
键入add-migration your_migration_name
完成创建新迁移后,输入update-database
再次运行
答案 1 :(得分:0)
我认为我必须理解为什么,但最终要使它起作用! 问题有效地存在于我的身份模型中:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("T_USER").Property(p => p.Id).HasColumnName("UserId");
}
}
我昨天删除了此行以清理代码,只需再次添加即可,一切都很好:
modelBuilder.Entity<IdentityUser>().ToTable("T_USER").Property(p => p.Id).HasColumnName("UserId");
因为我以为我不再使用IdentityUser了,所以我使用了ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string GetFullName(System.Security.Principal.IPrincipal usr)
{
var fullNameClaim = ((ClaimsIdentity)usr.Identity).FindFirst("FullName");
if (fullNameClaim != null)
return fullNameClaim.Value;
return "";
}
}
编辑:我精确地说我不是在使用代码优先方法,更新是在数据库中进行的,并且在同步edmx以后