Problem with EntityFramework 4.0 POCO proxy

I see that many people ask a similar question, but not this exact one. I am trying to do what I hope will be relatively simple with POCO proxies.

using (var context = new MyObjectContext()) { context.ContextOptions.ProxyCreationEnabled = true; // this does indeed create an instance of a proxy for me... // something like Product_SomeBunchOfNumbersForProxy var newEntity = context.CreateObject<MyEntity>(); // throws exception because newEntity is not in ObjectStateManager...why?? context.ObjectStateManager.GetObjectStateEntry(newEntity); // ok, well I guess let add it to the context manually... context.AddObject("Model.Products", newEntity); // doesn't throw an exception...I guess that good var state = context.ObjectStateManager.GetObjectStateEntry(newEntity); // prints System.Data.EntityState.Unchanged...oh...well that not good Console.WriteLine(state.State); // let try this... context.DetectChanges(); // doesn't throw an exception...I guess that good state = context.ObjectStateManager.GetObjectStateEntry(newEntity); // prints System.Data.EntityState.Unchanged...still no good... Console.WriteLine(state.State); // dunno, worth a shot... context.Refresh(RefreshMode.ClientWins); // throws exception because newEntity is not in ObjectStateManager... // that didn't help... state = context.ObjectStateManager.GetObjectStateEntry(newEntity); } 

What am I doing wrong? Thanks!

+4
source share
2 answers

It looks like you want to add a new proxy object so that EF can notice the changes.

As mentioned in StriplingWarrior CreateObject<T> , a proxied version of T is just created, you need to add it (for inserts) or Attach it (for updates) for EF to find out.

Reading between lines in code, it looks like you want to do an insert?

If this is true, a proxy is not even required.

Why?

Well, you do not need to track changes in the level of ownership (i.e., know what properties have been changed), all you need to know is that the object is new and needs to be inserted.

Add a call to ctx.MyEntities.Add(...) tells EF that.

This means that for inserts this is enough:

 var entity = new MyEntity(); ... // set some props ctx.MyEntities.Add(entity); ... // sets some more props ctx.SaveChanges(); 

Or that

 var entity = ctx.CreateObject<MyEntity>(); ... // set some props ctx.MyEntities.Add(entity); ... // sets some more props ctx.SaveChanges(); 

both will work. But the first is easier to understand.

Hope this helps

Alex (Microsoft)

+1
source

I haven't done much with proxies, but it looks like you expect it to track changes on an object that does not yet have state.

CreateEntity really just creates an object. This is no different from the โ€œnew MyEntityโ€. Therefore, you must add this entity to the context and save the changes before it has a true โ€œstateโ€ for tracking:

 using (var context = new MyObjectContext()) { context.ContextOptions.ProxyCreationEnabled = true; var newEntity = context.CreateObject<MyEntity>(); context.Add(newEntity); context.SaveChanges(); // now GetObjectStateEntry(newEntity) should work just fine. // ... } 
+1
source

All Articles