I can prevent the same object from being added twice , but I still need to reference the pending id to add other related objects.
In the following, some disctinct items in SomeData might want to add the same person twice. I can track that, but I still need to add the distinct item informatio to the pending person. how do i manage this?
foreach(item i in SomeData)
{
var x = dc.People.Where(p.someprop==a...);
if (x=null)
{
Person p = new P(..);
dc.People.InsertOnSubmit(p);
}
...
...
}
dc.Submitchanges()
Basically you need to check to see if the entity is already attached to the context. Here is an example of how you can make this work:
public static void SafeAttachTo<T>(this ObjectContext context, string entitySetName, ref T entity) where T : class {
ObjectStateEntry entry;
bool isDetached;
if (context.ObjectStateManager.TryGetObjectStateEntry(context.CreateEntityKey(entitySetName, entity), out entry)) {
isDetached = entry.State == EntityState.Detached;
entity = (T) entry.Entity;
}
else
isDetached = true;
if (isDetached)
context.AttachTo(entitySetName, entity);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.