Back to: Entity Framework Tutorials For Begineers and Professionals
Database Initialization in Entity Framework Code First Approach:
In this article, I am going to discuss Database Initialization in Entity Framework Code First Approach with Examples. Please read our previous article where we discussed Default Code-First Conventions in Entity Framework.
Database Initialization in Entity Framework Code First Approach:
We have already discussed that the Entity Framework Code-First Approach creates a database automatically. Now, let us try to understand how the Entity Framework API decides the database name (what should be the name of the database) and server (where the database should be created) while initializing a database using the code-first approach. Please have a look at the following diagram which shows database initialization workflow based on the parameter passed to the DbContext class constructor from our context class i.e. the class which is derived from the DbContext class.
As per the above diagram, we can call the base class constructor i.e. the DbContext class constructor from our context class in the following three ways.
- Calling DbContext Class Constructor with No Parameter
- Calling DbContext Class Constructor by Passing Database Name
- Calling DbContext Class Constructor by Passing Connection String Name
Calling DbContext Class Constructor with No Parameter:
If we call the DbContext class constructor which does not take any parameter from our user-defined context class, then Entity Framework API will create a local database in your local SQLEXPRESS server with the name that matches our {Namespace}.{Context class name}. For a better understanding, please have a look at the following code of the Context class. In the below example, we are calling the DbContext class constructor which does not take any parameter. In this case, the Entity Framework API will automatically create a database with the name EFCodeFirstDemo. EFCodeFirstContext in your local SQLEXPRESS server.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext: DbContext { //Constructor calling the DbContext class 0-Argument Constructor public EFCodeFirstContext() : base() { } //Adding Domain Classes as DbSet public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } }
Now, run the above code and then check the database name. If you look at the database name, then you will see that EFCodeFirstDemo is the namespace and EFCodeFirstContext is the context class name. To see the created database in the local SQLEXPRESS server, select SQL Server Object Explorer from the View menu in Visual Studio as shown in the below image.
This will open the following SQL Server Object Explorer window. Here, you can see the database name which is the same as the {Namespace}.{Context class name} i.e. EFCodeFirstDemo. EFCodeFirstContext is shown in the below image.
Calling DbContext Class Constructor by Passing Database Name
If we pass the database name as a parameter to the DbContext Class Constructor from our context class, then Entity Framework Code First Approach will create a database automatically again in the local SQLEXPRESS database server. But this time the name will be the one that is passed as a parameter to the DbContext Class constructor. For a better understanding, please have a look at the following context class code. Here, we are passing the name MyContextDB to the base class constructor. So, in this case, when we run the application, the Entity Framework API will automatically create a database with the name MyContextDB in the local SQLEXPRESS database server.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext: DbContext { //Constructor calling the DbContext class Constructor //by passing the database name as a parameter public EFCodeFirstContext() : base("MyContextDB") { } //Adding Domain Classes as DbSet public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } }
Now, run the above application and then check the database name. If you look at the database name, then you will see that the database name is MyContextDB. To see the newly created database in your local SQLEXPRESS server, select SQL Server Object Explorer from the View menu in Visual Studio as shown in the below image.
This will open the following SQL Server Object Explorer window. Here, you can see the database name MyContextDB as shown in the below image.
Calling DbContext Class Constructor by Passing Connection String Name
You can also define the connection string in the app.config or web.config file and specify the connection string name starting with “name=” in the base constructor of the context class. So, this is an easy way to tell the DbContext class to use a database server instead of local SQL Express or LocalDb.
Approach1:
If the name of the connection string matches the name of your context class (either with or without namespace qualification), then it will be found by DbContext when the parameterless constructor is used. Let us understand this with an example. Please modify the context class as shown below.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { //Constructor calling the DbContext class 0-Argument Constructor public EFCodeFirstContext() : base() { } //Adding Domain Classes as DbSet public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } }
As you can see in the above code, we are using the 0-Argument base class constructor. And the context class name is EFCodeFirstContext. Now, let us add a connection string in the App.Config file as we are working with a Console Application. In the case of a web application, you need to add the connection string in the web.config file. So, please add the following connection string section inside the configuration section.
<connectionStrings> <add name = "EFCodeFirstContext" connectionString = "Data Source =LAPTOP-ICA2LCQL\SQLEXPRESS;Initial Catalog = EFCodeFirstContextDB;Integrated Security = true" providerName = "System.Data.SqlClient"/> </connectionStrings>
Here, you can notice the connection string name is the same as the context class name i.e. EFCodeFirstContext. So, in this case, the Entity Framework API will not create the database in the local DB, instead, the 0-Argument constructor of the context class will use the above connection string and will create the database in the specified server which you specify in the connection string. Now, run the above code and verify the SQL Server and you should see a database created with the name EFCodeFirstContextDB as we specify the EFCodeFirstContextDB as the value of Initial Catalog as shown in the below image.
Approach2:
If the connection string name is different from the name of your context class, then you need to tell the DbContext class to use the connection string in Entity Framework Code First Approach by passing the connection string name as a parameter to the DbContext class constructor. The connection string name must start with “name=” otherwise, it will consider as a database name. So, first, modify the context class as shown in the below code.
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { //Constructor calling the DbContext class Constructor //by passing the connection string name using name= public EFCodeFirstContext() : base("name=MyConnectionString") { } //Adding Domain Classes as DbSet public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } } }
Now, we need to add a connection string in the app.config file with the name as MyConnectionString. An exception will be thrown if a connection string with the given name is not found in the config file. So, add the following connection string in the app.config file.
<connectionStrings> <add name = "MyConnectionString" connectionString = "Data Source =LAPTOP-ICA2LCQL\SQLEXPRESS;Initial Catalog = MyConnectionStringDB;Integrated Security = true" providerName = "System.Data.SqlClient"/> </connectionStrings>
As you can see in the above connection string, the name of the connection string is MyConnectionString and the database name is MyConnectionStringDB. So, when you run the application code, the Entity Framework API will use the above connection string and will automatically create a database in the specified server with the name MyConnectionStringDB. Once you run the code, you can also verify the database name in the specified server and you should the database as shown in the below image.
In the next article, I am going to discuss Database Initialization Strategies in Entity Framework Code-First Approach with Examples. Here, in this article, I try to explain Database Initialization in Entity Framework Code First Approach with Examples and I hope you enjoyed this Database Initialization in Entity Framework Code First Approach article. Please give your valuable feedback and suggestions about this article.