ADO.NET Entity: Insert Update and Delete

ADO.NET Entity: Insert Update and Delete

Rate This
  • Comments 28

For small blogs, it requires more references and explanation, which sometimes are redundant. So I thought of writing single blog which is combination of topics more or less related to one thing.

 

I am exploring ADO.NET Entity Framework and have been trying things out there. Here I am planning to demonstrate how to do insert, update and delete.

 

Here I will use a database created by me. In the Emp table there are two columns Id (Primary and auto-generated) and Name (simple varchar(50)).

image

Now I will create TestDB.edmx out of this database.

image

Insert

 

using (TestDBEntities ctx = new TestDBEntities())

{

    //Create new Emp object

    Emp e = new Emp() { Name = "Test Employee" };

 

    //Add to memory

    ctx.AddToEmp(e);

 

    //Save to database

    ctx.SaveChanges();

}

 

 

Update

 

using (TestDBEntities ctx = new TestDBEntities())

{

    //Get the specific employee from Database

    Emp e = (from e1 in ctx.Emp

             where e1.Name == "Test Employee"

             select e1).First();

 

    //Change the Employee Name in memory

    e.Name = "Changed Name";

 

    //Save to database

    ctx.SaveChanges();

}

 

Delete

 

using (TestDBEntities ctx = new TestDBEntities())

{

    //Get the specific employee from Database

    Emp e = (from e1 in ctx.Emp

             where e1.Name == "Test Employee"

             select e1).First();

 

    //Delete it from memory

    ctx.DeleteObject(e);

 

    //Save to database

    ctx.SaveChanges();

}

 

In my next post I will write about “how to handle CRUD with Relationship”.

Namoskar!!!

Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • PingBack from http://hubsfunnywallpaper.cn/?p=1790

  • SEGContainer objDB;

    Tema objTema;

    objTema = new Tema ();

    objTema.ID = pdecTemaID;

    objDB = new SEGContainer();

    objDB.DeleteObject (objDB.GetObjectByKey (objDB.CreateEntityKey ("Tema", objTema)));

    objDB.SaveChanges();

  • This are really nice simple examples! I'm gonna follow your blog posts to learn it step by step!

  • I would like to see how you handle crud with Relationship.

  • Few days back I had written an article on Insert/Update/Delete for simple standalone tables at ADO.NET

  • Hello JC,

    Took time but I have written one for you at http://blogs.msdn.com/wriju/archive/2008/10/16/ado-net-entity-insert-update-and-delete-with-relationship.aspx

  • I have been working with the ADO.NET Entities Data Model for ORM and I am very impressed. I'd been

  • Nice one. I was banging my head against the wall until I read this part:

    ctx.AddToEmp(e);

    That will catch a lot of people out.

  • thanks, this is helpful for those of us just getting our feet wet.  any chance of a post demonstrating calling a stored procedure with either input and/or output parameter examples?

  • Thanks for the blog post.  There isn't much good documentation on the ado.net entity framework.  This little page has helped me out alot.

    I did figure out another type of update.  

    Changing the foreign key of a row:

    //get the record you want to update

    var job = (from j in ptgDB.tbl_Job

                          where j.ID == jobid

                          select j).First();

    //in my DB, job_status is a foreign key

    //I select the new foreign key, and then assign it

    job.tbl_Job_Status = (from tbs in ptgDB.tbl_Job_Status

                                     where tbs.Title == "Working"

                                     select tbs).First();

  • Hi Wriju,

    Thanks a lot. This article has helped me really well.

    I have a query with regards to update. there will be scenarios where we will need to update multiple records on single submit.

    Could be through transaction scope or bulk update. So we can say like mulitple records or entities updation on the single submit.

    How can we do this without affecting the performance.

  • Thanks a lot Brian Boltan, i had been searching for this a whole day...........

  • Not understand please give me detail infomation

  • Hryey thanks a lot!!! This has helped me a lot in my work...

  • I want to update an entity but i dont want to retrieve it at save time and update it. I have its primary key in hidden field. Ho wto do that.????????

Page 1 of 2 (28 items) 12