ASP.NET MVC 5 - Entity Framework Database First Approach

Introduction 

 
Entity Framework is a gateway between your application and your database. A simple reason why Entity Framework stands out is that it automatically generates object classes that map with your database tables. In ASP.NET MVC5 platform entity framework also has the ability to generate complex types of object mapping classes that correspond with your database store procedures using entity framework database first approach.
 
Today, I shall be demonstrating Entity Framework database-first approach with ASP.NET MVC5 platform.
 
ASP.NET MVC 5 - Entity Framework Database First Approach
 
Prerequisites
 
Following are some prerequisites before you proceed any further in this tutorial.
  1. Knowledge of ASP.NET MVC5.
  2. Knowledge of HTML.
  3. Knowledge of JavaScript.
  4. Knowledge of Bootstrap.
  5. Knowledge of Jquery.
  6. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2017 Professional. I have taken the data sample from AdventureWorks for SQL server 2014
 
Let's begin now.

Step 1
 
Create a new MVC web project and name it "MVCDatabaseFirst".  

Step 2
 
Although you can create your entity framework model inside any hierarchy of the project, I prefer the "Models" folder, to keep code cleaner. So, right-click on your "Models" folder and then click "Add->New Item".
ASP.NET MVC 5 - Entity Framework Database First Approach
 
Step 3
 
Then, choose "ADO.NET Entity Data Model", name it "DataAcessModel" and click "Add".
 
ASP.NET MVC 5 - Entity Framework Database First Approach
 
Step 4
 
Now, from "Entity Data Model Wizard" choose "EF Designer from database", since I am creating Entity Framework database-first approach model. Then, click "Next".
ASP.NET MVC 5 - Entity Framework Database First Approach
Step 5
 
On the "Choose Your Data Connection" window, click "New Connection" button.
ASP.NET MVC 5 - Entity Framework Database First Approach
Step 6
 
In "Connection Properties" window, provide your SQL Server connection configuration. Then, click "OK". Your database connection string will be stored in the "Web.config" file.
ASP.NET MVC 5 - Entity Framework Database First Approach
Step 7
 
Now, on "Choose Your Data Connection" window, click "Yes, include sensitive data in the connection string." option and click "Next" as shown below.
ASP.NET MVC 5 - Entity Framework Database First Approach
Step 8
 
Now, on "Choose Your Database Objects and Settings" window, choose your target database objects. In my case, I have selected only the store procedures. Click "Finish".
ASP.NET MVC 5 - Entity Framework Database First Approach
Step 9
 
Ignore the below-shown security warning and click OK.
 
ASP.NET MVC 5 - Entity Framework Database First Approach
 
You can see that your target mapping object classes and related methods have been created automatically by Entity Framework.
 
ASP.NET MVC 5 - Entity Framework Database First Approach
 
Step 10
 
To open your "Entity Data Model Browser" window, click "View->Other Windows->Entity Data Model Browser".
 
ASP.NET MVC 5 - Entity Framework Database First ApproachASP.NET MVC 5 - Entity Framework Database First Approach
 
Step 11
 
Now, create a new "Controllers\HomeController.cs" file and create GET Index(...) method and POST Index(...)  and update the following lines of code in them.
  1. ...  
  2.   
  3. public ActionResult Index(int productId = 0)  
  4. {  
  5.     ...  
  6.   
  7.         // Verification.  
  8.         if (model.ProductID > 0)  
  9.         {  
  10.             // Settings.  
  11.             var details = this.databaseManager.GetProductByID(model.ProductID);  
  12.             model.ProductDetail = details.First();  
  13.         }  
  14.   
  15.         // Settings.  
  16.         model.ProductsGreaterThan1000 = this.databaseManager.GetProductByPriceGreaterThan1000().ToList();  
  17.   
  18.     ...  
  19. }  
  20.   
  21. ...  
  22.   
  23. [HttpPost]  
  24. [AllowAnonymous]  
  25. [ValidateAntiForgeryToken]  
  26. public ActionResult Index(ProductViewModel model)  
  27. {  
  28.     ...  
  29.   
  30.         // Verification  
  31.         if (ModelState.IsValid)  
  32.         {  
  33.             // Info.  
  34.             return this.RedirectToAction("Index""Home"new { productId = model.ProductID });  
  35.         }  
  36.   
  37.     ...  

In the above code, in the GET Index(...) method, I am simply verifying if the product ID is greater than zero than the get product ID based product details from the database. I am also displaying the list of products from the database into my web user interface. In the POST Index(...) method, I am simply redirecting the product ID value enter by the end-user to my GET Index(...) action method.

Step 12
 
Now, execute the project and you will be able to see the following in action.
ASP.NET MVC 5 - Entity Framework Database First Approach
ASP.NET MVC 5 - Entity Framework Database First Approach
 

Conclusion

 
In this article, you learned to create the application using Entity Framework database first approach with ASP.NET MVC5 platform. You learned to redirect your post requests to the GET action method, and to configure your SQL Server connection string in Entity Framework data model wizard.
 
Also, we saw how to open our "Entity Data Model Browser" window and utilize your Entity Framework auto-generated methods and object classes.


Similar Articles