标签:
使用VS2013创建了MVC4项目以后,在filters中添加了类InitializeSimpleMembershipAttribute ,不能识别UserContext和WebSecurity,发现缺少WebMatrix.WebData的引用,在Nuget控制台中中运行:install-package Microsoft.AspNet.WebPages.Data即可添加,这样解决了WebSecurity的问题。对于UserContext,需要在model中添加一个AccountModel的类。
public class InitializeSimpleMembershipAttribute : ActionFilterAttribute { private static SimpleMembershipInitializer _initializer; private static object _initializerLock = new object(); private static bool _isInitialized; public override void OnActionExecuting(ActionExecutingContext filterContext) { // Ensure ASP.NET Simple Membership is initialized only once per app start LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } private class SimpleMembershipInitializer { public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized.", ex); } } } }
标签:
原文地址:http://www.cnblogs.com/csts/p/MVC.html