private void ChangeRelationshipStates( DbContext context, EntityType entityType, object entity, EntityState state) { ObjectStateManager objectStateManager = ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager; foreach (AssociationType associationType in this._metadataWorkspace.GetItems <AssociationType>(DataSpace.CSpace).Where <AssociationType>((Func <AssociationType, bool>)(at => { if (at.IsForeignKey || at.IsManyToMany()) { return(false); } if (!at.SourceEnd.GetEntityType().IsAssignableFrom((EdmType)entityType)) { return(at.TargetEnd.GetEntityType().IsAssignableFrom((EdmType)entityType)); } return(true); }))) { AssociationEndMember principalEnd; AssociationEndMember dependentEnd; if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd)) { principalEnd = associationType.SourceEnd; dependentEnd = associationType.TargetEnd; } if (dependentEnd.GetEntityType().IsAssignableFrom((EdmType)entityType)) { EntityType entityType1 = principalEnd.GetEntityType(); Type clrType = EntityTypeExtensions.GetClrType(entityType1); DbSet set = context.Set(clrType); object obj1 = set.Local.Cast <object>().SingleOrDefault <object>(); if (obj1 == null || object.ReferenceEquals(entity, obj1) && state == EntityState.Added) { obj1 = this.InstantiateEntity(entityType1, context, clrType, set); ModificationCommandTreeGenerator.SetFakeReferenceKeyValues(obj1, entityType1); set.Attach(obj1); } if (principalEnd.IsRequired() && state == EntityState.Modified) { object obj2 = this.InstantiateEntity(entityType1, context, clrType, set); ModificationCommandTreeGenerator.SetFakeKeyValues(obj2, entityType1); set.Attach(obj2); objectStateManager.ChangeRelationshipState(entity, obj2, associationType.FullName, principalEnd.Name, EntityState.Deleted); } objectStateManager.ChangeRelationshipState(entity, obj1, associationType.FullName, principalEnd.Name, state == EntityState.Deleted ? state : EntityState.Added); } } }
public override void ChangeState(EntityState state) { EntityUtil.CheckValidStateForChangeRelationshipState(state, "state"); if (State == EntityState.Detached && state == EntityState.Detached) { return; } ValidateState(); if (RelationshipWrapper.Key0 == Key0) { ObjectStateManager.ChangeRelationshipState( Key0, Key1, RelationshipWrapper.AssociationSet.ElementType.FullName, RelationshipWrapper.AssociationEndMembers[1].Name, state); } else { Debug.Assert(RelationshipWrapper.Key0 == Key1, "invalid relationship"); ObjectStateManager.ChangeRelationshipState( Key0, Key1, RelationshipWrapper.AssociationSet.ElementType.FullName, RelationshipWrapper.AssociationEndMembers[0].Name, state); } }
private IEnumerable <TCommandTree> GenerateAssociation <TCommandTree>( string associationIdentity, EntityState state) where TCommandTree : DbCommandTree { AssociationType associationType = this._metadataWorkspace.GetItem <AssociationType>(associationIdentity, DataSpace.CSpace); using (DbContext context = this.CreateContext()) { EntityType sourceEntityType = associationType.SourceEnd.GetEntityType(); object sourceEntity = this.InstantiateAndAttachEntity(sourceEntityType, context); EntityType targetEntityType = associationType.TargetEnd.GetEntityType(); object targetEntity = sourceEntityType.GetRootType() == targetEntityType.GetRootType() ? sourceEntity : this.InstantiateAndAttachEntity(targetEntityType, context); ObjectStateManager objectStateManager = ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager; objectStateManager.ChangeRelationshipState(sourceEntity, targetEntity, associationType.FullName, associationType.TargetEnd.Name, state == EntityState.Deleted ? state : EntityState.Added); using (CommandTracer commandTracer = new CommandTracer(context)) { context.SaveChanges(); foreach (DbCommandTree commandTree in commandTracer.CommandTrees) { yield return((TCommandTree)commandTree); } } } }