A database is an organized collection of structured information, or data, typically stored electronically in a computer system. There are several types of databases that can be used in real world applications. Let’s talk about relational databases (representing data in the form of table). While creating the database, developers adapt various approach as per their requirements. Let’s talk about database first approach.
In database first approach, the database and tables are created first. Then the entity data model is created using the created database. It is easier to map and create the keys and the relationships as the developer does not have to write them in code. Therefore, this approach is suitable for large applications that rely highly on data.
Code First Approach:
In the code first approach, the developer first creates the entity classes with the required properties. Then the entity framework creates database and tables according to those defined classes. This approach is more suitable for small scale applications.
Model First Approach:
In the model first approach you can create the Entity Data Model (EDM) first, then generate the database from it. Entities definition and their relationships are created using designer in Visual Studio. If neither the domain classes nor the database is ready, then model first approach is the go-to solution.
Preferred for intense and large applications.
Good for small applications.
Preferred when the domain classes nor the database is ready.
The database is created with raw queries.
Database is created using scaffolding.
Database is created using a GUI tool.
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.
What is DbContext?
The DBContext class is basically used by our application to interact with the underlying database. That means this class is used to manage the database connection as well as also used to perform CRUD operations with the underlying database.
In order to perform any useful task by the DbContext class, we need an instance of the DbContextOptions class. The instance of the DbContextOptions carries all the required configuration information such as the connection string, database provider, etc.
What is DbSet?
A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type.
DBSet is an abstraction which is literally a collection of objects (usually lazily loaded) stored in a table. Operations done on DBSet are actually performed on the actual database records via SQL queries.
Let’s create a university database with database first approach
To create a database –
To create tables,
As database creation is completed, let’s create our mvc app to scaffold models.
To create a new mvc app with dotnet cli
dotnet new mvc –o DbFirst
For scaffolding and integrating with the database we need some nuget packages to be installed. Let’s install those packages using dotnet cli.
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Once the packages are installed, create dbcontext for our application.
A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit.
Once the dbcontext is created, then create a model with scaffolding. Enter the following command for scaffolding.
dotnet ef dbcontext scaffold "Server=VIVOBOOK\SQLEXPRESS;Database=University;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
This command will add fluent API in the context file and create the model files for the tables as shown in screenshot.
Created model files are
Finally, which approach to use?
These days, it’s a fairly simple question.
- If you are starting a new project with a new database, use EF core code first approach.
- If you are starting a new project, with an existing database you want to use EF core database first approach.