Reputation: 167
In Visual Studio 2013, I created an ASP .NET MVC web project with ASP.Net Identity.
And the Entity Framework Code First created a SQL Server Express LocalDB db in
APP_Data folder by default.
But I prefer a SQL Server Express db because SQL Server Express LocalDB couldn't work with IIS when I hosted the web app in local IIS.
So my questions are,
How can I create a SQL Server Express db rather than a SQL Server Express LocalDB db by default when I create a new project with EF Code First?
I have installed SQL Server Express even I have replaced "(LocalDB)\v11.0" with ".\SQLEXPRESS " in "Tools - Options - Data tools - Data connection - Instance name" of VS2013. But they didn't work for me.
Let us say now we have to use SQL Server Express LocalDB for ASP.Net Identity and my db, I wonder whether there will be an easy way to convert my LocalDB db to SQL Server Express or SQL Server db when I finish development and deploy my app to IIS.
Thank you in advance.
Upvotes: 2
Views: 1101
Reputation: 167
After so many times trying, I found the answers myself and would like to share here.
Just modify "(LocalDB)\v11.0" to ".\SQLEXPRESS" for value of DefaultConnection in web.config and grant enough permissions for the context login id in advance of generating of DB(in case of update-database command in NuGet PkgMgr Console, or running your web app in vs).
You may attach LocalDB file to SQL Server Express instance.
Hope to be helpful for someone.
Upvotes: 0
Reputation: 17039
Specify the connection string name in your code first DbContext
class like this:
public class MyDbContext : DbContext
{
public MyDbContext(): base("name=connectionString")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Order> Orders { get; set; }
}
Then inside the Web.config file specify the connection string to point to which ever version of SQL server that you desire:
<connectionStrings>
<add name="connectionString" connectionString="Password=password;Persist Security Info=True;User ID=sa;Initial Catalog=DatabaseName;Data Source=ServerName"
providerName="System.Data.SqlClient" />
</connectionStrings>
Upvotes: 2