Before we start implementing an ASP.NET MVC application using Entity Framework, lets first understand few related concepts. Microsoft Entity Framework is basically an ORM (Object Relational Mapping) framework. For a more detailed Entity Framework Tutorial for beginners, follow here.
So, what is an ORM?
In most of the cases when working with database driven application, we are performing different kind of operations on data like CRUD (Create, Retrieve, Update, Delete) operations. We might be using high level languages which are mostly object oriented like C#, VB.NET or Java etc. On the other hand, our databases are mostly relational in nature like MS SQL Server and Oracle. So, we are working with two different and incompatible type systems (i.e. Object Oriented Programming Languages with Relational Databases). So:
An ORM (Object Relational Mapping) framework provides an abstraction layer (a kind of mapping) between these two incompatible systems. It facilitates us to interact with data in an object oriented way and that’s what Microsoft Entity Framework is doing.
It can be illustrated by following diagram:
For the purpose of implementation, I already have created two tables in database (i.e. Category and Product) that we will be using to interact with. Let’s follow a step by step approach to build an ASP.NET MVC application with Entity Framework.
Create ASP.NET MVC Application using Visual Studio 2013 or 2015
Open Visual Studio 2013 or Visual Studio 2015 and first of all, let’s create an ASP.NET MVC application “MyProductCatalog” from menu File -> New Project.
On pressing “OK” button, an ASP.NET MVC application will be created as follows. We already have done it in more details in another ASP.NET MVC 5 Tutorial here.
Generate Entity Data Model
Now, we need to create an Entity Data Model in our ASP.NET MVC application.
What is an Entity Data Model? As we discuss above that our databases are relational in nature, so EDM (Entity Data Model) describes our database tables and their associations in object oriented way as Entities and association between these entities.
In order to create our EDM (Entity Data Model), right click on Models folder in our solution “MyProductCatalog” and choose “Add -> ADO.NET Entity Data Model”. Name it as ProductCatalogModel. Further choose “Generate from database” option as follows:
Choose or Create a database connection and name it as ProductCatalogEntities as given below and press Next:
We have two database tables so choose the tables and click “Finish” button.
We can see the newly generated EDM (Entity Data Model) as follows:
We have successfully created EDM (Entity Data Model) for our Product Catalog application. In following post on this blog, we will see how to communicate with this EDM and generate view.
Hopefully, this ASP.NET MVC 5 tutorial provided answers to following questions nicely:
- What is an ORM?
- Entity Framework as an ORM?
- What is an EDM (Entity Data Model) and how to create one?
More You Must Read about ASP.NET MVC & Related
- ASP.NET MVC3 Vs MVC4 Vs MVC5 Vs MVC6
- 4 Simple Steps to Create your First ASP.NET MVC5 Application
- Understanding ASP.NET MVC Application Life Cycle
- How Routing works in ASP.NET MVC?
- All you need to Know about Controllers and Action Methods in ASP.NET MVC
- Practical Example to learn Scaffolding in ASP.NET MVC
- How to use AJAX in ASP.NET MVC Application?
- Insight of ASP.NET MVC Authorize attribute
- Building ASP.NET MVC5 Application with Entity Framework 6
- Understanding Model-First Approach in ASP.NET MVC with Entity Framework
- ASP.NET View Engine Vs Razor View Engine
- ViewBag Vs ViewData Vs TempData Vs Session
- How to use Model to Pass data to View in ASP.NET MVC?
- Understanding Partial View in ASP.NET MVC
- ASP.NET MVC Helpers – A MUST KNOW
- 2 simple ways to create Custom HTML Helpers in ASP.NET MVC
- All you need to know to pass Exam: 70-486 (Developing ASP.NET MVC Web Applications)
- Free Practical Guide to ASP.NET Web API
- Difference between WCF and ASMX web service
- WCF Hosting (Console | Windows Service)
Top 10 Interview Questions and Answers Series:
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions