Search code examples
.netasp.net-mvc-4simplemembership

Do I have to initialize the database for simple membership or not? MVC contradictiving itself?


Ok, so I setup SimpleMembership, and I was under the impression that I had to run WebSecurity.InitializeDatabaseConnection(...) only once in order to have it set up the database. Later, when I attempt to look up a user id, I get an error telling me that I need to initialize the database before I ever call WebSecurity. Ok... so I put the initialization call back into _ViewStart.cshtml as it suggested. Now I run again and I get the error The "WebSecurity.InitializeDatabaseConnection" method can be called only once. So I'm damned if I do damned if I don't.

Can someone explain this to me?


Solution

  • You need to place the WebSecurity.InitializeDatabaseConnection(...) in the DatabaseInit method of the DatabaseConfig App_Start\DatabaseConfig.cs class like below:

        public static void DatabaseInit(string ConnectionName = "DefaultConnection", string userTable = "UserProfile", string userIdColumn = "UserId", string userNameColumn = "UserName", bool autoCreateTable = true)
        {
            WebSecurity.InitializeDatabaseConnection(ConnectionName, userTable, userIdColumn, userNameColumn, autoCreateTables: autoCreateTable);
        }
    

    Hope this helps :)