In this post I will walkthrough you on performing basic CRUD operation using ADO.Net Entity framework.

Database design

I am going to use School Database. You can find School database script here . To be precise, I am going perform CRUD operation on only one table Person. Schema of Person table is as below.

image

I am going to perform CRUD Operation on Person table from a console application.

Create Data Model

  1. Right click on Console Application and add new Item
  2. From Data Tab select ADO.Net Entity Data Model .If you want you can change name of the model.

image

3.   Click on Add button; you will get dialog box. From there select Generate from database option

image

4.  Create a new connection

image

5. Give database server name and choose database from the drop down as given below,

image

After clicking on Ok button, you will notice that you come to previous dialog box and a connection setting got created. Name of the connection setting is as below,

Database name + Entities = default connection setting name

If database is School then default connection setting name would be schoolentites. However, if you want you can change default connection setting name as desired.

image

6. Now you need to choose Data objects to make them part of Data Model. From below dialog box you can choose tables, stored procedures and views.

Since we are going to perform CRUD operation only on Person table , so I will choose only Person table to be part of Data Model.

image

If you want you can change Model Namespace name. By default it would be Database name suffixed with Model.

Now click Finish to create data model.

Performing CRUD operation

Retrieving all the records

You can fetch all the records as below.

image

Add a person

You can add a person in three steps

  1. Create object of Person
  2. Call add object on pluralize Person entity. In this case this is People
  3. Call save changes on entities

image

Modify a Person

To modify a person you need to perform three steps

  1. Fetch the Person to be modified
  2. Modify the desired values of the columns
  3. Call save changes on the entities

image

Delete a Person

To modify a person you need to perform three steps

  1. Fetch the Person to be deleted
  2. Call delete object on entities
  3. Call save changes on the entities

image

Full source code is given below,


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ADONetEntityModelfirstDemo
{
    class Program
    {
        static void Main(string[] args)
        {


            #region Reterive a Person

            SchoolEntities entities = new SchoolEntities();
            var result = from r in entities.People select r;
            foreach (var r in result)
            {
                Console.WriteLine(r.PersonID + r.LastName);
            }

            Console.ReadKey(true);

            #endregion

            #region Add a Pesron
            entities.People.AddObject(new Person
                                       { FirstName = "dhananjay",
                                         LastName = "kumar"
                                       });
            entities.SaveChanges();


            #endregion
            #region Modify a Person

            Person personToModofy = (from r in entities.People.Where
                                         (a => a.PersonID == 1)
                                         select r).FirstOrDefault();
            personToModofy.LastName = "Jamshedpur";
            entities.SaveChanges();

#endregion

            #region Delete  a Person

            Person personToDelete = (from r in entities.People.Where
                                        (a => a.PersonID == 1)
                                     select r).FirstOrDefault();
            entities.People.DeleteObject(personToDelete);
            entities.SaveChanges();


            #endregion
            #region Reterive  a Person

            var result1 = from r in entities.People select r;
            foreach (var r in result1)
            {
                Console.WriteLine(r.PersonID + r.LastName);
            }
            Console.ReadKey(true);
        }

#endregion
    }
}


In further post, we will discuss other aspects of ADO.Net Entity framework. I hope this post was useful. Thanks for reading  Smile

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Advertisements

5 thoughts on “Learn ADO.Net Entity Framework: Performing basic CRUD Operation

  1. Not able to call method .SaveChanges();
    Error:
    {“Unable to update the EntitySet ‘TPersons’ because it has a DefiningQuery and no element exists in the element to support the current operation.”}

    Could you please help.
    I have been to your presentation in Kolkata on 2nd July.
    Thanks for reply,
    Deepak

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s