private void GenericUpdateEntityCollection <T>(EntityCollection <T> collection, ObjectContext dbContext) where T : EntityObject, new() { int count = collection.Count(); int current = 0; List <T> collectionItemList = collection.ToList(); bool isAdded = false; while (current < count) { Object obj = null; dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj); if (obj == null) { obj = new AgentAirlineMappings(); ((T)obj).EntityKey = collectionItemList[current].EntityKey; dbContext.AddObject(((T)obj).EntityKey.EntitySetName, obj); dbContext.TryGetObjectByKey(collectionItemList[current].EntityKey, out obj); dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified); collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Modified); isAdded = true; } if (obj != null) { dbContext.ApplyCurrentValues <T>(((T)obj).EntityKey.EntitySetName, collectionItemList[current]); if (isAdded) { dbContext.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Added); collection.CreateSourceQuery().Context.ObjectStateManager.ChangeObjectState(collectionItemList[current], System.Data.EntityState.Added); } } current++; } }
public static ObjectQuery <T> CreateSourceQuery <T>(this ICollection <T> iCollection) where T : class { EntityCollection <T> entityCollection = iCollection as EntityCollection <T>; if (entityCollection == null) { throw new ArgumentNullException(); } return(entityCollection.CreateSourceQuery()); }