In this article, I will cover the two most popular approaches used by the programmers in domain driven applications. The main difference between Code First approach and Database First approach is that the Code First enables you to write entity classes and its properties first without creating the database design first. Whereas, in Database First approach, you create database and tables first, then generate entities using the database. You can choose an approach based on the application that you are going to develop.
What is Code First Database ?
In Code First approach, entities or classes are created first with the primary focus on the domain of an application. You can start creating classes and required properties, without designing the database that matches the entities. Then the Entity Framework creates the tables and database accordingly and when the code is run, the database is created.
Let us assume that you need to create a simple application to store the basic information of walk-in customers in the restaurant. Instead of designing the database first, you can start it by creating Customer class by using the following code. After running the code, you will see that the record of a customer will be saved in the database.
namespace TestApp
{
public class Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public DateTime? DateOfBirth { get; set; }
public DateTime? VisitedOn { get; set; }
}
public class ResturantContext: DbContext
{
public ResturantContext(): base()
{
}
public DbSet Customers { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
using (var dbCtx = new ResturantContext())
{
var customer = new Customer() { CustomerID=1,
CustomerName = " Customer A",
DateOfBirth= "####-##-##",
VisitedOn="####-##-##"};
dbCtx.Customers.Add(customer);
dbCtx.SaveChanges();
}
}
}
}
What is Database First Database ?
In Database First approach, database and the related tables are created first. After that, you can create an entity data models using database. It is easier to create a database, as there are multiple options are available by using graphical user interfaces.
To create a project using Database First approach, you need to create an empty project and add the Entity Framework package either from console or from Package Manager, to create the models from tables. You need to add Entity Data Model using ADO.Net Entity Model to create the connection string and after that, you need to choose the database objects for database context classes. Entity Framework will generate the code automatically.
public class Program
{
public static void Main(string[] args)
{
// access Context Class from Auto Generated Code
using (var dbCtx = new CustomerContext())
{
dbCtx.Customers.Add(new Customer { CustomerID=1,
CustomerName = " Customer A",
DateOfBirth= "####-##-##",
VisitedOn="####-##-##"});
dbCtx.SaveChanges();
foreach (var customer in dbCtx.Customers)
{
Console.WriteLine(customer.CustomerName);
}
}
}
}
Advantages and Disadvantages of Code First Database
Advantages
- You can create a database and required tables from business entities
- It is recommended for small applications that does not involve extensive data processing
- You can specify the collections for eager loading and the serialization of data
- It provides full access over the code and you can do modifications easily in the code
Disadvantages
- You need to write the code related to the creation of database
- If there is any change in database after the creation, you need to do it in the business entity class of code and run the application to update the database or by using package manager console
- It is difficult to manage the database through code, therefore, it is not recommended in data extensive applications where you need to process large amount of data and have complex logics to builup or maintain the data
Any manual changes will be lost if you update the code from application
Advantages and Disadvantages of Database First Database
Advantages
- Graphical User Interfaces are available to create database and tables, which makes the process easier
- It is preferred for the large and extensive data-driven applications
- It is easier to create keys and relationships without writing extra code for it
- It can use an existing database
- Visual Studio provides easy access to configure database via edmx files
Disadvantages
- If there is any change in database, model class needs to be extended with the same properties
- Creating and managing of keys and relationships requires more coding
- It is difficult to maintain or update edmx file if the database is large