Friday, 20 September 2013

Entity framework with CRUD Operations example

Hi Friends,

In this article, I will first explain what is Entity framework and what are the advantages of Entity framework and then I will try to explain how we can implement Entity Framework. So we will create ASP.NET project which do database transactions using Entity Framework

What is Entity Framework? 

  1.    What is Entity Framework?
ADO.NET is a very strong framework for data access. Entity Framework is an Object Relational Mapper (ORM). It basically generates business objects and entities according to the database tables and provides the mechanism for: Performing basic CRUD (Create, Read, Update, and Delete) operations. Easily managing "1 to 1", "1 to many", and "many to many" relationships. Ability to have inheritance relationships between entities.

    2.      Benefits of Entity Framework

We can have all data access logic written in higher level languages. The conceptual model can be represented in a better way by using relationships among entities. The underlying data store can be replaced without much overhead since all data access logic is present at a higher level.

3       3.     Architecture of Entity Framework


Entity Framework Example

Step 1: Create Your Database

Let's have a simple database with one table. Let's create a simple table for Contacts and we will perform CRUD operations on this table


Step 2: Adding the Entity Model to the Project

Create new Project and add the New Item ADO.NET Entity Data Model


Step 3: Once we select to add this data model to our website, we will have to select the approach we want to take for our Model's contents.


Step 4: Select Database

Step 5: Select Database and give Model namespace

Step 6: Then click on finish You will see Edmx Model


CRUD Operations using

1.       Insert operation

Design the form as given below

On click of add button please write below code

Contact con = new Contact();
con.fname = TextBox1.Text;
con.lname = TextBox2.Text; = TextBox3.Text;
ContactsDb db = new ContactsDb();

2.       Reading all the records

Add the datagrid as shown in the below screen