ADO.NET Entity: Insert Update and Delete
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)).
Now I will create TestDB.edmx out of this database.
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!!!
Comments
Anonymous
August 21, 2008
PingBack from http://hubsfunnywallpaper.cn/?p=1790Anonymous
September 03, 2008
SEGContainer objDB;Tema objTema;objTema = new Tema ();objTema.ID = pdecTemaID;objDB = new SEGContainer();objDB.DeleteObject (objDB.GetObjectByKey (objDB.CreateEntityKey ("Tema", objTema)));objDB.SaveChanges();Anonymous
September 03, 2008
public static void Eliminar (decimal pdecTemaID){
}SEGContainer objDB;Tema objTema;objTema = new Tema ();objTema.ID = pdecTemaID;objDB = new SEGContainer();//objDB.DeleteObject ((from T in objDB.Tema where T.ID == pdecTemaID select T).First ());objDB.DeleteObject (objDB.GetObjectByKey (new EntityKey (objDB.DefaultContainerName + ".Tema", "ID", pdecTemaID)));objDB.SaveChanges();
Anonymous
September 07, 2008
This are really nice simple examples! I'm gonna follow your blog posts to learn it step by step!Anonymous
September 29, 2008
I would like to see how you handle crud with Relationship.Anonymous
October 15, 2008
Few days back I had written an article on Insert/Update/Delete for simple standalone tables at ADO.NETAnonymous
October 15, 2008
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.aspxAnonymous
December 08, 2008
I have been working with the ADO.NET Entities Data Model for ORM and I am very impressed. I'd beenAnonymous
May 15, 2009
The comment has been removedAnonymous
June 23, 2009
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?Anonymous
September 06, 2009
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 updatevar 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 itjob.tbl_Job_Status = (from tbs in ptgDB.tbl_Job_Status where tbs.Title == "Working" select tbs).First();Anonymous
November 16, 2009
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.Anonymous
January 22, 2010
Thanks a lot Brian Boltan, i had been searching for this a whole day...........Anonymous
May 09, 2010
Not understand please give me detail infomationAnonymous
August 05, 2010
Hryey thanks a lot!!! This has helped me a lot in my work...Anonymous
May 30, 2011
The comment has been removedAnonymous
February 23, 2012
Hi Wriju, Can I update multiple employees through your update query ?Anonymous
March 29, 2012
this example is easy to understand. thanksAnonymous
May 20, 2012
good workAnonymous
August 01, 2012
Great to the point examples. Thank you.Anonymous
September 06, 2012
DeleteObject does not work in EF5Anonymous
November 11, 2012
Just wanted to say how great I thought your tutorial was. Its really helped me a lot. Thank you for writing itAnonymous
January 13, 2013
There is no such method as ctx.AddToEmp()Anonymous
January 14, 2013
It gets created based on the Entity so if you have an Entity "Dept" there will be a method AddToDeptAnonymous
September 26, 2013
Thanks a lot. This article has helped me really well.Anonymous
November 20, 2013
hw can i upadate no of records at one time.....Anonymous
February 17, 2014
Its very simple and very clear....Thanks for this post...Anonymous
July 09, 2014
I m using VS-2012 when i m using context. i m not get the deleteobject() methodAnonymous
August 10, 2014
Thank you man!Simple and excelent samples!Anonymous
June 08, 2015
The comment has been removedAnonymous
September 25, 2015
I have question. I am create form that store data into database table after click on save. But when i try to delete record after save the record it delete the Record but it generate new Record with same values with new id. so how i resolve it..Anonymous
October 14, 2016
Thanks its awesome working but delete object got error in context file i created internal method in context file but they giving exception so how can it resolve......- Anonymous
October 21, 2016
Do you have any referential integrity which is preventing delete? The easiest way is to get the generated SQL and run it manually. Another option could be Access rights to the database.
- Anonymous