InvalidOperationException - 帮助!!! [英] InvalidOperationException - Help!!!
问题描述
受保护
您好!
我收到以下错误:
对象处于分离状态。分离对象时,无法在ObjectStateEntry上执行此操作。
当我尝试此操作时:
{
foreach ( var 条目 in ObjectStateManager.GetObjectStateEntries( EntityState .Added | EntityState 。删除| EntityState .Modified | EntityState .Unchanged))
{
if (条目。实体!= null )< - - 这是我得到错误的地方
{
Detach(entry.Entity);
}
base .Dispose(disposing);
我在这里找到了这个简单的代码论坛,因为即使在处理了上下文之后,实体仍然依附于它。
它工作正常,直到我需要这样做:
public 列表 < Period > GetPeriod( string model, string company)
{
<使用 ( 实体 context = new 实体 ())
{
返回 Period .ReadPeriod(context,model,company);
}< - 这里调用dispose
}
public static List < Period > ReadPeriod( 实体 context, string modelName, string company)
{
var query = from p < font color ="#0000ff"size = 2> in context.Periods
join m in context.Models 上 p.Models.ID 等于 m.ID
join <字体大小= 2> c in context .Companies on p.Companies.ID equals c.ID
其中 m.Name == modelName& &安培; c.Name == company
选择 p;
return query.ToList< Period >( );
}
如果我删除了要处置的覆盖,它可以工作,但旧问题会在以后再次出现。
那么......我该怎么做才能避免这种情况?
Marco,
显然你不需要分离它已分离。
因此你只需要在分离之前添加另一个检查即
if ((entry.EntityState& ; EntityState.Detached)!= EntityState.Detached&& entry.Entity!= null )< - --thi s是我得到错误的地方
{
Detach(entry.Entity);
}
然后,如果EntityState被分离则会抛出entry.Entity中的Validation逻辑发生。结果,干杯结果亚历
protected
Hello!
I get the following error:
The object is in a detached state. This operation cannot be performed on an ObjectStateEntry when the object is detached.
when i try this:
{
foreach (var entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged))
{
if (entry.Entity != null) <---this is where i get the error
{
Detach(entry.Entity);
}
}
base.Dispose(disposing);
}
I found this simple code here on the forum, because even after disposing the context, entities still remain attached to it.
It worked fine till i needed to do this:
public List<Period> GetPeriod(string model, string company)
{
using (Entities context = new Entities())
{
return Period.ReadPeriod(context, model, company);
}<-- here where dispose is called
}
public static List<Period> ReadPeriod(Entities context, string modelName, string company)
{
var query = from p in context.Periods
join m in context.Models on p.Models.ID equals m.ID
join c in context.Companies on p.Companies.ID equals c.ID
where m.Name == modelName && c.Name == company
select p;
return query.ToList<Period>();
}
If i delete the override to dispose, it works, but the old problem appears again later.
So...what can i do to avoid this?
Marco,
Obviously you don't need to Detach if it is already Detached.
Hence you just need to add another check before Detaching i.e.
if ((entry.EntityState & EntityState.Detached) != EntityState.Detached && entry.Entity != null ) <---this is where i get the error
{
Detach(entry.Entity);
}
And then the Validation logic inside entry.Entity which throws if the EntityState is Detached doesn't happen.
Cheers
Alex
这篇关于InvalidOperationException - 帮助!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!