Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Building Your First MVC Application with Entity Framework

0.00/5 (No votes)
27 Jun 2014CPOL3 min read 11.4K  
How to build your first MVC application with Entity Framework

Before we start implementing an ASP.NET MVC application using Entity Framework, let's first understand few related concepts. Microsoft Entity Framework is basically an ORM (Object Relational Mapping) framework. So, what is an ORM?

In most of the cases when working with database driven applications, we are performing different kinds 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 Microsoft 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 the following diagram:

ADO.NET Entity Framework

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

Firstly, let’s create an ASP.NET MVC application “MyProductCatalog” from menu File -> New Project.

ASP.NET MVC

ASP.NET MVC

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 Tutorial here.

ASP.NET MVC App

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:

Entity Data Model Step 1

Choose or create a database connection and name it as ProductCatalogEntities as given below and press Next:

Entity Data Model Step 2

We have two database tables so choose the tables and click “Finish” button.

Entity Data Model Step 3

We can see the newly generated EDM (Entity Data Model) as follows:

EDM Diagram

We have successfully created EDM (Entity Data Model) for our Product Catalog application. In following posts on this blog, we will see how to communicate with this EDM and generate view.

Hopefully, this ASP.NET MVC tutorial provided answers to the following questions nicely:

  • What is an ORM?
  • Entity Framework as an ORM?
  • What is an EDM (Entity Data Model) and how to create one?

Other Related Articles

The post Building your first MVC application with Entity Framework appeared first on Web Development Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)