2

I am able to add data, but not sure how should I update the data. I am getting AddObject,DeleteObject methods not found any method to update.

Thanks

2 Answers 2

5

You simply grab an (or multiple) object(s), manipulate them and call SaveChanges on the context. Of course, the object has to be attached to the context and tracking must enabled.

var obj = context.table.First(o => o.ID == 1);
obj.Property1 = data;
context.SaveChanges();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply I tried this but not working Investor inv = context.Investors.First(i => i.InvestorId == new Guid(investorId)); if (inv != null) { inv.InvestorName = tbInvestorName.Value; context.SaveChanges(); }
Hey Sorry My Mistake it was some diferent error..Thanks its working
Gives "The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship." error
Crystal Balling: The property you are changing has a key relationship with another model, but you aren't changing that model with the same value.
5

Taken from Employee Info Starter Kit, you can consider the code snippet as below:

public void UpdateEmployee(Employee updatedEmployee)
        {
            //attaching and making ready for parsistance
            if (updatedEmployee.EntityState == EntityState.Detached)
                _DatabaseContext.Employees.Attach(updatedEmployee);
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
            _DatabaseContext.SaveChanges();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.