ASP.NET MVC5: Entity Framework Database First Approach
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 maps with your database tables. In ASP.NET MVC5 platform entity framework also has the ability to generate complex types 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.
Following are some prerequisites before you proceed any further in this tutorial:
1) Create a new MVC web project and name it "MVCDatabaseFirst".
2) Although you can create your entity framework model inside any hierarchy of the project. But, I prefer "Models" folder, to keep code cleaner. So, right click on your "Models" folder and then click "Add->New Item" as shown below i.e.
3) Then choose "ADO.NET Entity Data Model", name it "DataAcessModel" and click "Add" as shown below i.e.
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" as shown below i.e.
5) On the "Choose Your Data Connection" window, click "New Connection" button as shown below i.e.
6) Then in "Connection Properties" window provide your SQL server connection configurations and then click "OK" as shown below i.e. your database connection string will be stored in "Web.config" file.
7) Now, on "Choose Your Data Connection" window, click "Yes, include sensitive data in connection string." option and click "Next" as shown below i.e.
8) Now, on "Choose Your Database Objects and Settings" window, choose your target database objects. In my case, I have select only the store procedures. Then click "Finish" as shown below i.e.
9) Ignore below shown security warning and click OK as shown below i.e.
You can see that your target mapping object classes and related methods have been created automatically by entity framework as shown below i.e.
10) To open your "Entity Data Model Browser" window, click "View->Other Windows->Entity Data Model Browser" as shown below i.e.
11) Now, create a new "Controllers\HomeController.cs" file and create GET Index(...) method and POST Index(...) and update following lines of code in them i.e.
In the above code, In GET Index(...) method, I am simply verifying if the product ID is greater than zero then get product ID base 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.
12) Now, execute the project and you will be able to see the following in action i.e.
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.
Download Now!
Let's begin now.1) Create a new MVC web project and name it "MVCDatabaseFirst".
2) Although you can create your entity framework model inside any hierarchy of the project. But, I prefer "Models" folder, to keep code cleaner. So, right click on your "Models" folder and then click "Add->New Item" as shown below i.e.
3) Then choose "ADO.NET Entity Data Model", name it "DataAcessModel" and click "Add" as shown below i.e.
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" as shown below i.e.
5) On the "Choose Your Data Connection" window, click "New Connection" button as shown below i.e.
6) Then in "Connection Properties" window provide your SQL server connection configurations and then click "OK" as shown below i.e. your database connection string will be stored in "Web.config" file.
7) Now, on "Choose Your Data Connection" window, click "Yes, include sensitive data in connection string." option and click "Next" as shown below i.e.
8) Now, on "Choose Your Database Objects and Settings" window, choose your target database objects. In my case, I have select only the store procedures. Then click "Finish" as shown below i.e.
9) Ignore below shown security warning and click OK as shown below i.e.
You can see that your target mapping object classes and related methods have been created automatically by entity framework as shown below i.e.
10) To open your "Entity Data Model Browser" window, click "View->Other Windows->Entity Data Model Browser" as shown below i.e.
11) Now, create a new "Controllers\HomeController.cs" file and create GET Index(...) method and POST Index(...) and update following lines of code in them i.e.
... public ActionResult Index(int productId = 0) { ... // Verification. if (model.ProductID > 0) { // Settings. var details = this.databaseManager.GetProductByID(model.ProductID); model.ProductDetail = details.First(); } // Settings. model.ProductsGreaterThan1000 = this.databaseManager.GetProductByPriceGreaterThan1000().ToList(); ... } ... [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Index(ProductViewModel model) { ... // Verification if (ModelState.IsValid) { // Info. return this.RedirectToAction("Index", "Home", new { productId = model.ProductID }); } ... }
In the above code, In GET Index(...) method, I am simply verifying if the product ID is greater than zero then get product ID base 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.
12) Now, execute the project and you will be able to see the following in action i.e.
Post a Comment