Search code examples
c#asp.net-mvc-4simplemembership

Websecurity not using correct database


Im building a simple webb app using VS2012, SQLExplress, latest .net version, Code first entity framework and migrations. Im building from the standard MVC4 template in VS2012 which is using the new simple membership as default.

My problem is that I cant get Websecurity class to use the database I have created. I use this code in my Glabal.asax.cs:

WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"UserProfile",
"UserId",
"UserName",
autoCreateTables: true);

Ive tried to run this snippet in the migration seed method as well but with the same result.

My connectionstring looks like this

<add name="DefaultConnection" connectionString="Data Source=localhost\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" />

When I run the initializer with the autoCreateTables: true parameter the initalizer creates a new connection that is only visible in VS2012 like this.

This pic is showing a connection called SecurityConnection2 as I tried to force websecurity to use another connection string but the behaviour is the same as it would have been using the DefaultConnection. As you can see it creates all the tables within this new connection and I cant find tose tables anywhere in the database.

If I instead use autoCreateTables: false the initializer will throw an error saying that the UserProfile table does not exist.

In all examples and tutorials Ive read this just works out of the box but I just cant get it to work.

There is probably a simpe solution to this but I fail to see it and I would be grateful for any kind of help here.


Solution

  • The answer to the problem was that the connection string didn't contain an "initial catalog".