Familiarize yourself with the ASP.NET MVC Database first
Following the introduction to the first code approach in ASP.NET today I will continue to work on the first database approach
I. Concept of First Code in ASP.NET MVC
Database First means building the database first and then building the application source code. Database First allows us to reverse a model from a database that already exists. This model is saved in a file called EDMX (.edmx) and can be viewed and edited in the Entity Framework Designer (Entity Framework designer). The layers you interact with in the application are automatically generated from the EDMX file.
This is an approach that is commonly used in large systems where a project has a dedicated database design department.
To find out more in detail, I will make an example below
II. Create an ASP.NET MVC application with the Database First approach
Step 1. Create a project
In this article I use Visual studio 2019 and SQL Sever 2014 Proceed and open and create 1 ASP .NET MVC proejct as follows:
Step 2. Set the data from the database
Because it is the database first, of course I will database and tables offline ^^
First we will open SQL Sever and create a new Database named BlogDbFirst
Then create an Employee table as follows:
Step 3: Create models from the database
In this step we will generate modals from the tables available in the database that we will connect the following
First, right-click on the Models folder and select Add => New item => Data => ADO.NET Entity Model As follows:
In the next step, we will connect the newly created database to the project. You need to have 2 information: the Sever name of the Sql Sever and the Database name just created and configured as follows
After successful connection, the connection string will be created:
Note that after creating, I need to rebuild the project to use these models in the controller.
III. summary
- Advantages:
- Easy, familiar to almost everyone
- The code automatically generates based on the tables in the database
- Suitable for large applications
- Defect:
- Cannot manage database version
- Discomfort when working in groups, the whole team often have to share a server to unify the database for the whole team
Whether following the database first or the first code has its own advantages and disadvantages, you should apply it flexibly in real cases.