Vishwakant Tripathi

Vishwakant Tripathi

  • 1k
  • 721
  • 839.7k

ASP.NET Simple Membership database could not be initialized

Sep 7 2017 12:32 PM
Hi, 
I am working on a MVC application, in which I am trying to use Simple Membership method. It is working fine when I run application through Visual Studio, but It gives error "ASP.NET Simple Membership database could not be initialized" when I publish and deploy it in IIS.
Below is part of my code what I am using...
 
My connection string:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DBName;User ID=sa;Password=password123;" /> 
 
AppSetting: 
<add key="enableSimpleMembership" value="true" />
 
Filter--> InitializeSimpleMembershipAttribute.cs:
[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);
}
public 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);
}
}
}
}
 
 

Answers (2)