How to create database on SQL Server 2017 instead of LocalDB or SQL Express via Code-First Entity Framework approach?

asp.net-mvc c# entity-framework entity-framework-6 sql-server-2017

Question

Actually, the title already summarizes my whole problem. I'm using Visual Studio 2017 and Entity Framework 6. In my MVC application, when I follow code-first approach, with default settings I was only given options of either LocalDB or SQL Server Express to use in the SQL Server Object Explorer. I am able to see corresponding tables for my models in action using SQL Express.

However, I want to use my SQL Server 2017. I guess I must specify my desired DB in connection string but I could not figure out how to do that with Entity Framework 6.

1
1
12/16/2017 5:16:00 PM

Accepted Answer

You can just open your project web.config file, or the app.config file, then update your entity framework connection string to refer to the SQL Server instance you need just like below:

 <add name="DefaultConnection" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=YourServerInstance;Initial Catalog=YourDB;Persist Security Info=True;User ID=sa;Password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Make sure that you are editing the right connection string by checking your context class to check which connection string key name it refers:

public YourContext(bool lasyLoadingEnabled) : base("DefaultConnection")
{
    Database.SetInitializer<NimasContext>(null);
    this.Configuration.LazyLoadingEnabled = lasyLoadingEnabled;
    this.Configuration.ProxyCreationEnabled = false; ;
}

You can follow this link: https://msdn.microsoft.com/en-us/library/jj556606(v=vs.113).aspx

1
12/16/2017 11:00:51 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow