Learn ADO.Net Entity Framework: Performing Basic CRUD Operation


This article is a walkthrough of performing basic CRUD operations using the ADO.Net Entity Framework.

Database design

I am going to use a School Database. You can find the School database script here. To be precise, I am going perform CRUD operations on only one table; Person. The schema of the Person table is as below.

ADOEntity1.gif

I am going to perform CRUD operations on the Person table from a console application.
 
Create Data Model

  1. Right-click on the console application and select "Add | New Item..."
  2. From the Data tab select ADO.Net Entity Data Model. If you want you can change the name of the model.

    ADOEntity2.gif
     
  3. Click the Add button; you will get a dialog box. From there select the "Generate from database" option:

    ADOEntity3.gif
     
  4. Create a new connection

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

    ADOEntity5.gif

    After clicking on Ok button, notice that you have returned to the previous dialog box and a connection setting has been created. The name of the connection setting is as below:

    Database name + Entities = default connection setting name

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

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

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

    ADOEntity7.gif

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

Now click Finish to create the data model.

Performing CRUD operation

Retrieving all the records

You can fetch all the records as below.

ADOEntity8.gif

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

    ADOEntity9.gif

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
    Call save changes on the entities

    ADOEntity10.gif

Delete a Person

To delete 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

    ADOEntity12.gif

The 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 a future article, we will discuss other aspects of the ADO.Net Entity framework. I hope this article was useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all