entity framework update code example

Example 1: entity save example in c# model first

using (var context = new BloggingContext())
{
    // seeding database
    context.Blogs.Add(new Blog { Url = "http://example.com/blog" });
    context.Blogs.Add(new Blog { Url = "http://example.com/another_blog" });
    context.SaveChanges();
}

using (var context = new BloggingContext())
{
    // add
    context.Blogs.Add(new Blog { Url = "http://example.com/blog_one" });
    context.Blogs.Add(new Blog { Url = "http://example.com/blog_two" });

    // update
    var firstBlog = context.Blogs.First();
    firstBlog.Url = "";

    // remove
    var lastBlog = context.Blogs.Last();
    context.Blogs.Remove(lastBlog);

    context.SaveChanges();
}

Example 2: how to update record using entity framework 5

BY LOVE SINGH.

employeeDBEntities.tblEmployee.Attach(objTblEmployee);
employeeDBEntities.Entry(objTblEmployee).State = EntityState.Modified;
employeeDBEntities.SaveChanges();

Example 3: how to update record using entity framework in c#

public void UpdateCustomer(Customer custDTO)
{
   CustomerEntities ce = new CustomerEntities();
   Customer cust = ce.Customers.Find(custDTO.Id);
   if (cust != null)
   {
    DbEntityEntry<Customer> ee = ctx.Entry(cust);

Example 4: entity save example in c# model first

using (var context = new BloggingContext())
{
    var blog = context.Blogs.First();
    blog.Url = "http://example.com/blog";
    context.SaveChanges();
}

Tags:

Misc Example