ASP.NET MVC5: Entity Framework Simple Code First Database Approach
There has been a lot of buzz about entity framework in tech world.
The reason is simple entity framework is an overwhelming development communicator between our physical database engines and our code base. Entity framework in ASP.NET MVC5 platform offers following three approaches to connect with database i.e.
- Code First Approach.
- Model First Approach.
- Database First Approach.
Following are some prerequisites before you proceed further in this tutorial:
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of Bootstrap.
- Knowledge of Jquery.
- Knowledge of C# Programming.
1) Create a new MVC project in visual studio and name it "EFCodeFirstMvc".
2) On "Models" folder, right click and click "New Item" as shown below i.e.
3) Now click "ADO.NET Entity Data Model" and name it "EFCodeFirstDbContext" as shown below i.e.
4) Choose "Empty Code First model" and click finish as shown below i.e.
You will see that "EFCodeFirstDbContext.cs" file has been created under Models folder. We will change it later in this tutorial.
5) Let's create our empty database without any tables into SQL server database engine, I am using below script to create an empty database named "db_code_first" i.e.
This script is auto-generated from SQL server, you need to replace "SQL Server DATA Path" with your SQL server data storage path in the above script.
6) On "View" menu click "Server Explorer" as shown below i.e.
7) Let's modify our database connection settings, so that, when we create our schema from code, it will automatically updated into SQL server database. On "Server Explorer" window right click "EFCodeFirstDbContext" connection and click "Modifying connection" as shown below i.e.
8) Provide your connection settings into connection settings window and click "OK" i.e.
You will notice that your database is empty and there is no table currently existing as shown below i.e.
9) Open the "EFCodeFirstDbContext.cs" file and replace below code in it i.e.
In the above code, we have created our table called "LoginEntities" and tells our DB context about our table with the following line i.e.
Notice in our class"LoginEntity", we have commented out last property. I will come back to this property when we perform table schema changes via code in code first approach.
10) Now, create a controller and named it "AccountController.cs" under "Controllers" folder and replace following code in it i.e.
In the above code, we have written both HTTP GET and HTTP POST methods for our "Register" action. You can see some commented out code in HTTP GET method, I will come back to it, while in the HTTP POST method, I have added a simple logic to add my account information into my database by using code first approach.
11) Create a new model called "AccountViewModel.cs" under "Models" folder and replace below code in it i.e.
The above code is a simple model, that I will attach with my account registration view.
12) Now create "Register.cshtml" file under "Views\Account" folder and replace following in it as shown below i.e.
In the above code, I have created a simple form for account registration and a result list which will display my data from "LoginEntities" table. You will see commented out property which we will come back to soon.
13) Let's first execute the project and create a sample account, you will see output as shown below i.e.
You will notice that there are two tables which have been created one is the table that we have defined at code level and other is the migration history table . The migration history table will keep the history version of the changes that you have made into database tables and its structure. When you expand your table, you will see your defined columns via code as shown below i.e.
15) Now, let's add a new property into our table via code and by entity framework provided migration commands, we will signal our physical database about schema changes. So, uncomment all the code that I have mentioned previously about being commented out in the models, views & controllers folders.
16) Let's signal our SQL server about this schema, before that make sure that your nuget package is installed, if not than install it via "Tools-> Extensions & Updates" as shown below i.e.
17) Now open "Package Manager Console" via "Tools->Package Manager Console" and type "Enable-Migrations" command and hit enter, you will see details as shown below i.e.
You will notice that a "Migration" folder has been created with a history version .cs file and "configuration.cs" file which will maintain migration history at code level and migration command settings in configuration file.
18) Now, enter "Add-Migration AddFullName" command and you will see result as shown below i.e.
In the above command, notice that at"AddFullName" portion we have written the name of our new column property after "Add" keyword i.e. FullName.
19) Finally, update our SQL server database about the schema changes by entering "Update-Database" command i.e.
20) Refresh the SQL server connection and you will see that our new column is being reflected in the SQL server database as shown below i.e.
21) Now, execute the project and register new account, you will see following result i.e.