我看到很多人提出了类似的问题,但并非这个确切的问题。我正在尝试使用POCO代理完成相对简单的操作。
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's add it to the context manually...
context.AddObject("Model.Products", newEntity);
// doesn't throw an exception...I guess that's good
var state = context.ObjectStateManager.GetObjectStateEntry(newEntity);
// prints System.Data.EntityState.Unchanged...oh...well that's not good
Console.WriteLine(state.State);
// let's try this...
context.DetectChanges();
// doesn't throw an exception...I guess that's 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);
}
我做错了什么?谢谢!