Database Connection in Entity Framework

In the EF Code First Example, we built a simple CRUD Application. The EF created and initialized the database, even though we did not specify any connection string. In this article, we will learn how Entity Framework discovers which database connection to use, and how we can modify it. 

Open the Project we used in the last tutorial. You can download it from this link

DBContext

The database connection to be used is determined by the parameter passed to the constructor of the DbContext class. There are two scenarios exists

  1. No Parameter
  2. NameOrConnectionString

No Parameter

In the above example, we have not passed anything in the Constructor of the DbContext class. In such a case, the entity framework will create the database either in localDB or SQL Express. The name of the database will be a fully qualified name of your derived DBContext class. This we looked at in our previous tutorial.

The following image shows the database created by Entity Framework.

Code First in Entity Framework
Code First in Entity Framework

In the above example, the name of the created database is EFGettingStarted.EFContext. The database connection to be used is decided by the entity framework configuration section in web.config (or app.config) file. The entity framework, when being installed via NuGet package manager checks whether the SQL Express or localDB is installed in the system. If SQL Express is installed, then it is used. If SQL Express is not present, then the localDB is used. Nuget package updates this information in the web.config file under the section DefaultConnectionFactory

The Configuration file for localdb looks like this

If you want to change it to the SQL Express then you change the connection to as shown below

Code First Default Connection Factory

The <defaultConnectionFactory> section is under the <entityFramework> section and is required and is used if you do not specify the connection string. The EF uses this section to locate the database to use for the context. You can use this section change any provider or you can use customer providers.

Name Or ConnectionString

The DbContext also takes the string constructor. You can either pass the name of the database or the connection string to the constructor

Name of the database

You can pass the name of the database in the constructor of the DbContext method. The rest of the process is similar to No Parameter. EF will create the database using the defaultConnectionFactory provided in the web.config file.

Open our project and change the DbContext constructor as follows

Run the application and you will see that the database is created with the name EFDatabase as shown in the image below

Entity Framework Code First Database in Connection string
Entity Framework Code First Database in Connection string

Connection String

You can pass the connection string to be used in the constructor using the syntax “name=YourConnectionString”. Here the YourConnectionString is your connection string. Please note that the difference between above and is that the parameter begins with “Name=”. If you omit this the EF will treat the string as the name of the database. For Example

In the above example “EFConstring” is your connection string. This connection String, then needs to be specified in the web.config file under the connectionStrings section

You can see from the image below that the database with the name EFConstring is created by the Entity Framework Code First

Entity Framework Code First Passing Database
Entity Framework Code First Passing Database

Conclusion

In this tutorial, we learned how Entity Framework discovers and creates the database based on the convention. In the next tutorial, we will look at how the Entity framework initializes the database

Download

Source Code
Here is the link to tutorials in this series

  1. Introduction to Entity Framework
  2. Basics of Entity Framework
  3. Introduction to Code First
  4. Entity Framework Code First Example
  5. ASP.NET MVC Code First Approach

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top