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.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial.
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of JavaScript.
- Knowledge of Bootstrap.
- Knowledge of Jquery.
- 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".
Step 3
Then, choose "ADO.NET Entity Data Model", name it "DataAcessModel" and click "Add".
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".
Step 5
On the "Choose Your Data Connection" window, click "New Connection" button.
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.
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.
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".
Step 9
Ignore the below-shown security warning and click OK.
You
can see that your target mapping object classes and related methods
have been created automatically by Entity Framework.
Step 10
To open your "Entity Data Model Browser" window, click "View->Other Windows->Entity Data Model Browser".
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.
- ...
-
- public ActionResult Index(int productId = 0)
- {
- ...
-
-
- if (model.ProductID > 0)
- {
-
- var details = this.databaseManager.GetProductByID(model.ProductID);
- model.ProductDetail = details.First();
- }
-
-
- model.ProductsGreaterThan1000 = this.databaseManager.GetProductByPriceGreaterThan1000().ToList();
-
- ...
- }
-
- ...
-
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult Index(ProductViewModel model)
- {
- ...
-
-
- if (ModelState.IsValid)
- {
-
- return this.RedirectToAction("Index", "Home", new { productId = model.ProductID });
- }
-
- ...
- }
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.
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.