title: .net MVC Simple Membership Authentication with Database
body: <p>Using Code First Entity Framework with .NET MVC 4 I have created a new Web Application and populated the database with object as shown below.</p>
  <pre><code> public class GratifyGamingContext : DbContext
  {
      public DbSet&lt;Game&gt; Games { get; set; }
      public DbSet&lt;Genre&gt; Genres { get; set; }
      public DbSet&lt;UserProfile&gt; UserRepository { get; set; }
  }
  </code></pre>
  <p>I want to use my UserRepository table instead of the inbuilt UserContext class from AccountModel.cs for my user account access since I can't get that the work with my existing dbContext.</p>
  <pre><code>    public class UsersContext : DbContext
  {
      public UsersContext()
          : base(&quot;DefaultConnection&quot;)
      {
      }
  
      public DbSet&lt;UserProfile&gt; UserProfiles { get; set; }
  }
  
  [Table(&quot;UserProfile&quot;)]
  public class UserProfile
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int UserId { get; set; }
      public string UserName { get; set; }
  }
  </code></pre>
  <p>My program always crashes when constructing a SimpleMembershipInitializer object from InitializeSimpleMembershipAttribute.cs. I have commented out the code I feel should be irrelevant.</p>
  <pre><code>    [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&lt;UsersContext&gt;(null);
  
              try
              {
                  using (var context = new GratifyGamingContext())
                  {
                      if (!context.Database.Exists())
                      {
                          // Create the SimpleMembership database without Entity Framework migration schema
                          ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                      }
                  }
  
                  WebSecurity.InitializeDatabaseConnection(&quot;DefaultConnection&quot;, &quot;UserProfile&quot;, &quot;UserId&quot;, &quot;UserName&quot;, autoCreateTables: true);
              }
              catch (Exception ex)
              {
                  throw new InvalidOperationException(&quot;The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588&quot;, ex);
              }
          }
      }
  }
  </code></pre>
  <p>My ConnectionString is as follows:</p>
  <pre><code>&lt;add name=&quot;DefaultConnection&quot; connectionString=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-GratifyGaming-20120917185558;AttachDbFilename=|DataDirectory|\Games.mdf;Integrated Security=True&quot; providerName=&quot;System.Data.SqlClient&quot; /&gt;
  </code></pre>
  <p>I get the following error when calling the WebSecurity.InitializeDatabaseConnect from any AccountController page:</p>
  <blockquote>
  <p>[SqlException (0x80131904): Directory lookup for the file
  &quot;C:\Users\Unreal\Documents\Visual Studio
  2010\Projects\GratifyGaming\GratifyGaming.WebUI\App_Data\Games.mdf&quot;
  failed with the operating system error 2(failed to retrieve text for
  this error. Reason: 15105). Cannot attach the file
  'C:\Users\Unreal\Documents\Visual Studio
  2010\Projects\GratifyGaming\GratifyGaming.WebUI\App_Data\Games.mdf' as
  database 'aspnet-GratifyGaming-20120917185558'.]</p>
  <p>[TargetInvocationException: Exception has been thrown by the target of
  an invocation.]    System.RuntimeTypeHandle.CreateInstance(RuntimeType
  type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached,
  RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck) +0</p>
  </blockquote>
  <p>My application is otherwise connected to the database if I do not go to any AccountController driven page.
  How can I configure this application to use my UserRepository table instead of the UsersContext for user membership?</p>