Change Connection String to SQL Server in Entity framework Code First

When you work with Microsoft entity framework code first approach Entity framework creates your database and put it some location according to its default behavior. But most of the time we want to see our database into our local SQL Server. So here I’m showing the simplest way to force the entity framework to create your database into SQL Server installed to the local system.

Open solution explorer. Press [Ctrl + Alt + L] to open solution explorer and find out the App.config file.

Open App.config file from solution explorer.


 

App.config file will be like this. See the space below the configSections. We’ll add our customized connection string to force our database to be create into our SQL Server.
                       

Add this code below the configSections.
                      

Here is the short description for this connection string:

  • name: Set the name for your database in SQL Server.

  • connectionString: This is the connection where all database connections information are stored.

  • Data Source = localhost: When you assign Data source as “localhost” this means that we’re going to connect to local instance of SQL Server in our local machine.

  • Initial Catalog = StudentDatabase: Initial catalog is the name of the context class what you’ve created into your code classes. Here I’ve set the context class name = “StudentDatabase”.

  • User Id = sa: User id is the user id to login into SQL Server.

  • Password = ****: Set the password which you use to login into SQL server on your pc. See the User Id and Password in SQL Server in picture.

  • ProviderName: Provider Name is used to specify the data provider for the application towards database. When you work with .NET application you’ll specify the provider name “System.Data.SqlClient”.

This is how you can force entity framework to create database into local SQL Server.