Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i been trying to update entity from its reference as explained below
iam getting this error
An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
how to make this happen if i dont want to deal with each entity alone
user[id name]
action[id text userid resultId]]
result[id resulttxt]
C#
public static void test1(action act) {
             act.text="132";
             act.user.name="test";
             act.result.resulttxt="test";
             testEntities e = new testEntities();
             e.actions.Attach(act);
             e.SaveChanges();
       }
          testEntities b = new testEntities();
          action act = b.actions.Include("user").Include("result").Where(x => x.id == 9).FirstOrDefault();
          test1(act);
Posted

Why are you creating a second DbContext inside your Test1 method? You shouldn't be because now you're trying to track the same entity using two different contexts and that doesn't work. Test1, since it didn't create a context and didn't add anything to the context to track, shouldn't be concerned at all with saving any changes. That should be left up to the caller that made the context instance.
 
Share this answer
 
you mean like this

C#
 public static void test1(action act) {       
              act.text="132";
              act.user.name="test";
              act.result.resulttxt="test";
                }
           testEntities b = new testEntities();
           action act = b.actions.Include("user").Include("result").Where(x => x.id == 9).FirstOrDefault();
 test1(act);
b.SaveChanges();


well yes it worked but why i cant attach that entity and save change in the other context
 
Share this answer
 
There is a blog on how to solve this exception http://www.admarketspace.com/Blog/Index. The source code is also available for download
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900