Saturday, June 18, 2016

ASP.NET MVC 5 From Scratch : Create Entity Data Model With Entity Framework

In our previous blogs we've created an ASP.NET MVC from scratch.  In this blog we are going to use Entity Framework as the ORM (Object Relational Mapping) as a conduit to our database, so that we can query our data as an object.  An ORM as the name implies maps database tables, views, and stored procedures as objects in a programming language so that developers can work with the data as objects.

Step-by-Step Instructions:

1.  First we need to add the Entity Framework 6.1.3 to our ASP.NET MVC, we accomplish by right-click on "References" then select "Manage NuGet Packages"











2. On the search box type in "EntityFramework" no spaces, in the search result click on the "Install" button next to the "EntityFramework" package.











3.  Click "I Accept" on the "Licence Acceptance" screen




























4. Click "Close"






5.  Once the "EntityFramework" package has been added you will see a green checkmark next to the "EntityFramework" search result.











6.  If you look at the "References" in the project you will now see the EntityFramework references










7.  Now we are ready add a database model to our project.  We will add the "Northwind" database to our project.  Right-click on the "Models" folder, then select "Add" → "New Item"




















8.  Select "Data" on the left pane under "C#" then select "ADO.NET Entity Data Model" the main pane















9. In the "Name" field for the item type "NorthwindModel"





10. Click the "Add" button





11.  On the "Choose Model Contents" screen, select "EF Designer from database"











12. On the "Connection Properties" screen, click on the "New Connection" button






13. In this screen select type in the SQL server name, or select it from the "Server name" dropdown list.  Then type "Northwind" or select it from the "Select or enter a database name" dropdown list.  Then click "OK".  If you don't have the Northwind database in your database server, you can follow the instructions in this blog to add it.








































14. All the database connection information should be configured correctly in the "Entity Data Model Wizard" now.  Click "Next"



15.  For the database objects to include, select "Tables", "Views", and "Stored Procedures and Functions" then click "Finish"


16.  The Entity Framework models and classes are now added to the project.

17.  If you click on the "NorthwindModel.edmx" file the model will be visually presented in the model designer editor

























18.  Build the project, and that's it

No comments:

Post a Comment