An entity object cannot be referenced by multiple instances of IEn

  • Thread starter Thread starter Dick
  • Start date Start date
D

Dick

I get the exception: “An entity object cannot be referenced by multiple
instances of IEntityChangeTracker†when I attempt to attach my object to my
context.

Here's my code

public Customer Fetch(int ID)
{
using (OIC2Entities context = new OIC2Entities())
{
Customer Customer = (from c in context.Customer where
c.ID.Equals(ID) select c).First();
Customer.Project.Load();
return Customer;
}
}

public void Save(Customer Customer)
{
using (OIC2Entities context = new OIC2Entities())
{
context.Attach(Customer);
ObjectStateEntry objectStateEntry =
context.ObjectStateManager.GetObjectStateEntry(Customer.EntityKey);
foreach (FieldMetadata fieldMetadata in
objectStateEntry.CurrentValues.DataRecordInfo.FieldMetadata)
{

objectStateEntry.SetModifiedProperty(fieldMetadata.FieldType.Name);
}
context.SaveChanges();
}
}
 
Hello Dick,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

When a query is executed inside an object context in the Entity Framework,
the returned objects are automatically attached to the object context, so
that the context can track all its changes. Even if the context has
disposed, the entity object still considers itself attached and cannot be
attached to another context. So to attach an object to a different
context, you have to detach the object from the original context first.

For example:

using (OIC2Entities context = new OIC2Entities())
{
Customer Customer = (from c in context.Customer where
c.ID.Equals(ID) select c).First();
Customer.Project.Load();
context.Detach(Customer);
return Customer;
}

For more information, you can refer to this document:

How to: Detach Objects from an Object Context (Entity Framework)
http://msdn.microsoft.com/en-us/library/bb738697.aspx

And the discussion in this thread:

http://forums.microsoft.com/msdn/showpost.aspx?postid=2971822&siteid=1&sb=0&
d=1&at=7&ft=11&tf=0&pageid=0

If you have any questions or concerns, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks for your response, however, if I call Detatch as you suggest, then
I'll lose the relationship between my parent and child entities, which is
clearly nonsense.

i.e.

using (OIC2Entities context = new OIC2Entities())
{
Customer Customer = (from c in context.Customer where
c.ID.Equals(ID) select c).First();
Customer.Project.Load();
Debug.Assert(Customer.Project.Count.Equals(1));
context.Detach(Customer);
Debug.Assert(Customer.Project.Count.Equals(0));
return Customer;
}
 
Dear Dick,

Why not declaring the ObjectContext object as global variable?

Anyway, if it's neccessary to use local variables, since objects attached
to one ObjectContext cannot be attached to another ObjectContext, you have
to detach them first, to retain the relationship, you can enumerate all
relations via ObjectContext.ObjectStateManager.GetObjectStateEntry(),
temporarily store the graph after the detach. And reconstructing the graph
of objects afterwards when needed. For more information, you can refer to
these documents:

ObjectStateManager..::.GetObjectStateEntry Method (EntityKey)
http://msdn.microsoft.com/en-us/library/bb155228.aspx

How to: Attach Related Objects (Entity Framework)
http://msdn.microsoft.com/en-us/library/bb896245.aspx

If you have any questions or concerns, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
My application is on the server. The entity objects are passed over WCF to
clients. The client passes the objects back if they have updates that need to
be persisted in the store. Hence the need to detatch and reattach.

Your suggestion sounds like a huge amount of work. I though EF was meant to
save us such efforts. Doesn't sound like EF is finished. I think returning to
datasets sounds like the best option.

Thanks anyway.
 
Back
Top