0

I'm trying to update Entity/model using LINQ to SQL (DBML). but I'm not able to do it.

Here is my code snippet.

public void Update(Customer customer)
{
    using (MyDataContext db = new MyDataContext())
    {
        db.Customers.Attach(customer, true);
        db.SubmitChanges();
    }
}

public Customer GetByID(int ID)
{
    using (MyDataContext db = new MyDataContext())
    {
        return db.Customers.FirstOrDefault(c => c.CustomerID == ID);
    }
}

My scenario is: I get the customer object and bind the customer object to form. and after change the form input data. Data is perfectly change but when I call update method. It's not updating it and I have this error:

An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.

I searched a lots of internet but I'm not able to find any appropriate solution. I also modified my update function like that:

public void Update(Customer customer)
{
    using (MyDataContext db = new MyDataContext())
    {
        var originalCustomer = db.Customers.FirstOrDefault(c => c.CustomerID == customer.CustomerID);
        db.Customers.Attach(customer, originalCustomer);
        db.SubmitChanges();
    }
}

But still getting the same error.

I don't want to get Customer from database and update it properties from parameter customer properties. I want to use the same parameter customer and update it in database.

Please help me. Thanks!

Saadi
  • 2,211
  • 4
  • 21
  • 50

3 Answers3

0

The problem is that even though you are using the same DbContext class you are using different DbContext objects because you are using two different "using" statements.

It's important that you use the same DbContext object throughout the request (assuming it's an MVC app).

The usual approach would be to instantiate the DbContext in the controller (or using Dependency Injection ) and use the reference everywhere.

Miguel Veloso
  • 1,055
  • 10
  • 16
0

The issue is you have two using statements, giving 2 different context The easier option would be to inject it into the controller and use it when needed

-1
public void Update(Customer customer)  
{  
using (MyDataContext db = new MyDataContext())  
{  
    var originalCustomer = db.Customers.Where(c => c.CustomerID == customer.CustomerID).FirstOrDefault();  
   // change the originalCustomer's properties with customer's properties. 
    db.SubmitChanges();  
}  
}
Mehmet
  • 739
  • 1
  • 6
  • 17
  • As I mentioned in my description, I don't want to get Customer from database and update it properties. Please read the description. Any how, Thanks for your answer. – Saadi Jun 19 '16 at 08:14
  • I do not understand why you want to use Attach().But if you use attach() method you must change a property of entity before savechanges().https://msdn.microsoft.com/en-us/data/jj592676.aspx – Mehmet Jun 19 '16 at 08:49
  • I have changed model object in customer which is passed as parameter. – Saadi Jun 19 '16 at 08:53
  • Try db.SaveChanges() instead of SubmitChanges() – Mehmet Jun 19 '16 at 08:59
  • There is no SaveChanges method for DBML. It's in EDMX. – Saadi Jun 19 '16 at 09:08