コード例 #1
0
        private void UpdateEntry <TEntity>(IEntityWrapper wrappedEntity, EntityEntry existingEntry)
        {
            Type type = typeof(TEntity);

            if (type != existingEntry.WrappedEntity.IdentityType)
            {
                EntityKey entityKey = existingEntry.EntityKey;
                throw new NotSupportedException(Strings.Materializer_RecyclingEntity((object)TypeHelpers.GetFullName(entityKey.EntityContainerName, entityKey.EntitySetName), (object)type.FullName, (object)existingEntry.WrappedEntity.IdentityType.FullName, (object)entityKey.ConcatKeyValue()));
            }
            if (EntityState.Added == existingEntry.State)
            {
                throw new InvalidOperationException(Strings.Materializer_AddedEntityAlreadyExists((object)existingEntry.EntityKey.ConcatKeyValue()));
            }
            if (this.MergeOption == MergeOption.AppendOnly)
            {
                return;
            }
            if (MergeOption.OverwriteChanges == this.MergeOption)
            {
                if (EntityState.Deleted == existingEntry.State)
                {
                    existingEntry.RevertDelete();
                }
                existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity);
                this.Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, true);
                existingEntry.AcceptChanges();
                this.Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, true);
            }
            else if (EntityState.Unchanged == existingEntry.State)
            {
                existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity);
                this.Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, true);
                existingEntry.AcceptChanges();
                this.Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, true);
            }
            else if (this.Context.ContextOptions.UseLegacyPreserveChangesBehavior)
            {
                existingEntry.UpdateRecordWithoutSetModified(wrappedEntity.Entity, (DbUpdatableDataRecord)existingEntry.EditableOriginalValues);
            }
            else
            {
                existingEntry.UpdateRecordWithSetModified(wrappedEntity.Entity, (DbUpdatableDataRecord)existingEntry.EditableOriginalValues);
            }
        }
コード例 #2
0
        public IEntityWrapper HandleEntityAppendOnly <TEntity>(
            Func <Shaper, IEntityWrapper> constructEntityDelegate,
            EntityKey entityKey,
            EntitySet entitySet)
        {
            IEntityWrapper entityWrapper;

            if ((object)entityKey == null)
            {
                entityWrapper = constructEntityDelegate(this);
                this.RegisterMaterializedEntityForEvent(entityWrapper);
            }
            else
            {
                EntityEntry entityEntry = this.Context.ObjectStateManager.FindEntityEntry(entityKey);
                if (entityEntry != null && !entityEntry.IsKeyEntry)
                {
                    if (typeof(TEntity) != entityEntry.WrappedEntity.IdentityType)
                    {
                        EntityKey entityKey1 = entityEntry.EntityKey;
                        throw new NotSupportedException(Strings.Materializer_RecyclingEntity((object)TypeHelpers.GetFullName(entityKey1.EntityContainerName, entityKey1.EntitySetName), (object)typeof(TEntity).FullName, (object)entityEntry.WrappedEntity.IdentityType.FullName, (object)entityKey1.ConcatKeyValue()));
                    }
                    if (EntityState.Added == entityEntry.State)
                    {
                        throw new InvalidOperationException(Strings.Materializer_AddedEntityAlreadyExists((object)entityEntry.EntityKey.ConcatKeyValue()));
                    }
                    entityWrapper = entityEntry.WrappedEntity;
                }
                else
                {
                    entityWrapper = constructEntityDelegate(this);
                    this.RegisterMaterializedEntityForEvent(entityWrapper);
                    if (entityEntry == null)
                    {
                        this.Context.ObjectStateManager.AddEntry(entityWrapper, entityKey, entitySet, "HandleEntity", false);
                    }
                    else
                    {
                        this.Context.ObjectStateManager.PromoteKeyEntry(entityEntry, entityWrapper, false, true, false);
                    }
                }
            }
            return(entityWrapper);
        }