Home > Entity framework, LINQ > ADO.NET ENTITY FRAMEWORK: Simple Insert Update Delete in Entity Framework

ADO.NET ENTITY FRAMEWORK: Simple Insert Update Delete in Entity Framework


Introduction

In this blog I am going to show you how to perform simple select, insert, update and delete operation using Entity Framework.

Entity framework  support three model to work with data in your database

  1. Database First
  2. Model First
  3. Code First

Database First: In database First your database first created and then we use that created database in my operation with Entity Framework.

Other two approach/model i will be posting in my next blog.As my aim is to keep this post as simple as possible so i will  be focusing more simple CRUD operation using Database first approach.

Step 1) let’s see database schema

This is the table in database against which I am going to do the CRUD operation

Step 2) create a project in visual studio and add EDM (Entity Data Model)

As you can see I have selected ADO.NET Entity Data Model this is main item to work with Entity framework, without it it’s impossible to work with Entity Framework

Now select Generate from database (this is called database first approach). There are two other code first and model first that I will cover in my upcoming blog post.

Over here you can give the name of entity model of which you will create object that will work as entry point of EMD.

Select your table, view Store procedure over here.

Step 3) let’s see EDM added in project

Step 4) Now Final CRUD operation will be done from here

Let’s start with insert operation

using (EFDemoEntities cntx = new EFDemoEntities())

{
//INSERTION
Employee emp = new Employee()
              { EFName = "Sujeet", ELName = "Kumar", EAddress = "kolkata", EMobile = "9999999999" };
cntx.AddToEmployees(emp);
//or
//cntx.AddObject("Employees", emp);

}

Now lets see how to select

using (EFDemoEntities cntx = new EFDemoEntities())
{
var emp = (from e in cntx.Employees
where e.EFName == "Sujeet"
select e).FirstOrDefault();
if (emp != null)
{
Console.WriteLine(emp.EFName);
Console.WriteLine(emp.ELName);
Console.WriteLine(emp.EAddress);
Console.WriteLine(emp.EMobile);
}
}

Now lets see how to update

using (EFDemoEntities cntx = new EFDemoEntities())
{
var emp = (from e in cntx.Employees
where e.EFName == "Sujeet"
select e).FirstOrDefault();

if(emp !=null)

{

emp.EFName="Sujit";

emp.ELName="Kumar Singh";

emp.EAddress="Kolkata City";

emp.EMobile = "9877777777";

}

cntx.SaveChanges();

}

Now lets see how to delete

using (EFDemoEntities cntx = new EFDemoEntities())
{

var emp = (from e in cntx.Employees
where e.EFName == "Sujit"
select e).FirstOrDefault();

cntx.DeleteObject(emp);
cntx.SaveChanges();
}

I tried my best to keep it simple as much as possible so that reader can take interest in this technology and at least get started.

Hope you will learn something new from this blog.

If you like this post or this blog site , please give me feedback ad like this blog Facebook page . it motivate and make me write more and more .

If you have any problem in understanding this technology then you can ask me .

Thanks for reading this post .


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: