我想使用MVC从SQL中的aspnetusers数据库锁定用户。 这是我的管理员控制器:
@using (Html.BeginForm("LockUser", "Admin", new { id = x.Id }))
{
<input type="submit" class="button" value="Lock" />
}
这是我的管理员观点:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The entity type ApplicationUser is not part of the model for the current context.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
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.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)
at System.Data.Entity.DbSet`1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)
at System.Data.Entity.DbSet`1.FindAsync(Object[] keyValues)
at Microsoft.AspNet.Identity.EntityFramework.EntityStore`1.GetByIdAsync(Object id)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<GetUserAggregateAsync>d__6c.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 Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
at Microsoft.AspNet.Identity.UserManagerExtensions.FindById[TUser,TKey](UserManager`2 manager, TKey userId)
at BlogMaxim.Controllers.AdminController.LockUser(String Id) in C:\nmct\2e Jaar\1e sem\project\BlogMaxim\BlogMaxim\Controllers\AdminController.cs:line 70
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
InnerException:
当我点击按钮时,我的数据库中没有任何反应,我仍然可以使用该帐户登录。 我收到这个错误:
std::string
答案 0 :(得分:0)
使用UserManager
进行更新时,请使用以下代码。您需要在数据库上下文中调用SaveChanges
方法以保持更改,否则它将不会在数据库中持久存在。
var datastore = new UserStore<ApplicationUser>();
var manager = new UserManager<ApplicationUser,string>( dataStore);
//update current user object in memory
//var user = User.Identity.GetApplicationUser();
var user = manager.FindById(Id)
user.LockoutEnabled = true;
user.LockoutEndDateUtc = DateTime.UtcNow.AddYears(2);
manager.UpdateAsync(user);
var dbcontext = datastore.Context;
//update user object in database
dbcontext.SaveChanges();
在代码顶部包含以下命名空间。此外,在这种情况下,您只需将光标放在红色下划线的左下角,然后单击图标即可显示Visual Studio的建议。
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
添加对以下程序集的引用:Microsoft.AspNet.Identity.Core.dll and Microsoft.AspNet.Identity.EntityFramework.dll