【发布时间】:2011-03-30 05:04:50
【问题描述】:
我看到很多人都在问类似的问题,但不是这个问题。我正在尝试做我希望使用 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);
}
我做错了什么?谢谢!
【问题讨论】:
-
我认为 newEntity 的一些属性更改应该改变它的状态,而不仅仅是它的创建。
-
我不确定你到底想做什么。你有很多代码,试图做一些事情。但目前还不清楚你真正想要做什么?
-
问题是:为什么我的新对象在我使用上下文创建它时没有附加到上下文 - 为什么当我手动添加它时它的状态没有改变?...它应该是新的...
标签: c# .net entity-framework entity-framework-4 poco