예제 #1
0
        private object InstantiateAndAttachEntity(EntityType entityType, DbContext context)
        {
            Type   clrType = EntityTypeExtensions.GetClrType(entityType);
            DbSet  set     = context.Set(clrType);
            object entity  = this.InstantiateEntity(entityType, context, clrType, set);

            ModificationCommandTreeGenerator.SetFakeReferenceKeyValues(entity, entityType);
            ModificationCommandTreeGenerator.SetFakeKeyValues(entity, entityType);
            set.Attach(entity);
            return(entity);
        }
예제 #2
0
        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);
                }
            }
        }