InitializeSimpleMembership Attribute and SimpleMembership Exceptions
来源:互联网 发布:php 调用jenkins 接口 编辑:程序博客网 时间:2024/05/01 20:21
原文:https://blogs.msdn.microsoft.com/rickandy/2012/08/15/initializesimplemembership-attribute-and-simplemembership-exceptions/
The InitializeSimpleMembership Attribute ensures that before any membership (login/register) related functionality is run, that the membership database has been created. If the database is not yet created, the code will automatically create one. If the simple membership initialization fails, the Web Application can continue to run requests that don’t require membership.
Simple membership initialization failure can occur for the following reasons.
- The most common reason is the connection string to SQL Server is not valid. For example, you might not have SQL Server available. This is a frequent cause of failure in Azure. The ASP.NET MVC 4 templates by default use SqlExpress when created with Visual Studio 2010 and LocalDB when using Visual Studio 2012. SqlExpress is not installed on Azure and LocalDB does not run on Azure.
- Multiple DBContext objects on the same database.
The following code shows the InitializeSimpleMembership Attribute code that is added to a new ASP.NET MVC application. The code is found in the Filters folder:
using System;using System.Data.Entity;using System.Data.Entity.Infrastructure;using System.Threading;using System.Web.Mvc;using WebMatrix.WebData;using MvcV4.Models;namespace MvcV4.Filters{ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public sealed 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. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } } } }}
#region // ////策略一:数据库不存在时重新创建数据库 //Database.SetInitializer<UsersContext>(new CreateDatabaseIfNotExists<UsersContext>()); ////策略二:每次启动应用程序时创建数据库 //Database.SetInitializer<UsersContext>(new DropCreateDatabaseAlways<UsersContext>()); ////策略三:模型更改时重新创建数据库 //Database.SetInitializer<UsersContext>(new DropCreateDatabaseIfModelChanges<UsersContext>()); ////策略四:从不创建数据库 //Database.SetInitializer<UsersContext>(null); #endregion
- InitializeSimpleMembership Attribute and SimpleMembership Exceptions
- SimpleMemberShip
- Transactions and Exceptions
- Exceptions and control flow
- SEH and C++ Exceptions
- Errors and Exceptions
- Python Errors and Exceptions
- [OS]Exceptions and Interrupts
- Python Errors and Exceptions
- Files and Exceptions
- Throwing and Catching Exceptions
- 10 Exceptions and Control
- SimpleMembership【转】
- Exceptions in Java and C#
- Python[十六]:Class and exceptions
- Interrupts, hardware exceptions, and signals
- C++ Exceptions: Pros and Cons
- Handling unhandled exceptions and signals
- PAT1042
- webrtc在ubuntu14.04上的下载和编译
- loadView详解
- 大数据项目2:内存受限的大数据预测模型
- 回归(regression)与分类(classification)的区别
- InitializeSimpleMembership Attribute and SimpleMembership Exceptions
- 执行StartExecution时, 报ActiveX component can't create object错误
- Activity的四种launchMode
- 那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)
- android getX()、getY()、getRawX()、getRawY()、getSrrollX()、getScrollY()、scrollTo()、scrollBy()对比
- 太空中十种最可怕死法:一个比一个吓人
- Xcode 6制作动态及静态Framework
- controller coding formats
- 例题6-12 UVa572 Oil Deposits(DFS)