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();
}
0 Comments:
Post a Comment
<< Home