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 Asp.net 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 Asp.net

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;
con.phone = TextBox3.Text;
ContactsDb db = new ContactsDb();
db.Contacts.AddObject(con);
db.SaveChanges();

2.       Reading all the records

 
Add the datagrid as shown in the below screen


 

We can retrieve the collection of Entities using the Model object to achieve this. 
The code snippet below will show how that can be done.


ContactsDb db = new ContactsDb();
datagrid.DataSource = db.Contacts;
datagrid.DataBind();

3.       Updating the record



If we want to update a record, then a simple update operation can be performed as:
int idToupdate = Convert.ToInt32(Request.QueryString["id"].ToString());
ContactsDb db = new ContactsDb();
Contact con = db.Contacts.SingleOrDefault(p => p.id == idToupdate); con.phone = TextBox1.Text;
db.SaveChanges();

4.       Deleting a record


If we want to delete a particular record then we can perform a delete operation by using the DeleteObject function. The following code snippet demonstrates the same:
//delete this contact int idToupdate = Convert.ToInt32(Request.QueryString["id"].ToString());
ContactsDb db = new ContactsDb();
Contact con = db.Contacts.SingleOrDefault(p => p.id == idToupdate); db.Contacts.DeleteObject(con);
db.SaveChanges();

Please find attached source code








Happy Programming!!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------