Пример #1
0
        /// <summary>
        /// Attaches an entity to the DataContext in either a modified or unmodified state.
        /// If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking.
        /// Deferred loading is not enabled. Other entities accessible from this entity are not automatically attached.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="asModified"></param>
        public void Attach(TEntity entity, bool asModified)
        {
            if (entity == null)
            {
                throw Error.ArgumentNull("entity");
            }
            CheckReadOnly();
            context.CheckNotInSubmitChanges();
            context.VerifyTrackingEnabled();
            MetaType type = this.metaTable.RowType.GetInheritanceType(entity.GetType());

            if (type == null)
            {
                throw Error.InheritanceTypeNotRegistered(entity.GetType());
            }
            if (!IsTrackableType(type))
            {
                throw Error.TypeCouldNotBeTracked(type.Type);
            }
            if (asModified)
            {
                bool canAttach = type.VersionMember != null || !type.HasUpdateCheck;
                if (!canAttach)
                {
                    throw Error.CannotAttachAsModifiedWithoutOriginalState();
                }
            }
            TrackedObject tracked = this.Context.Services.ChangeTracker.GetTrackedObject(entity);

            if (tracked == null || tracked.IsWeaklyTracked)
            {
                if (tracked == null)
                {
                    tracked = this.context.Services.ChangeTracker.Track(entity, true);
                }
                if (asModified)
                {
                    tracked.ConvertToModified();
                }
                else
                {
                    tracked.ConvertToUnmodified();
                }
                if (this.Context.Services.InsertLookupCachedObject(type, entity) != entity)
                {
                    throw new DuplicateKeyException(entity, Strings.CantAddAlreadyExistingKey);
                }
                tracked.InitializeDeferredLoaders();
            }
            else
            {
                throw Error.CannotAttachAlreadyExistingEntity();
            }
        }
Пример #2
0
        public void Attach(TEntity entity, bool asModified)
        {
            if (entity == null)
            {
                throw System.Data.Linq.Error.ArgumentNull("entity");
            }
            CheckReadOnly();
            Context.CheckNotInSubmitChanges();
            Context.VerifyTrackingEnabled();
            var inheritanceType = metaTable.RowType.GetInheritanceType(entity.GetType());

            if (!IsTrackableType(inheritanceType))
            {
                throw System.Data.Linq.Error.TypeCouldNotBeTracked(inheritanceType.Type);
            }
            if (asModified && inheritanceType.VersionMember == null && inheritanceType.HasUpdateCheck)
            {
                throw System.Data.Linq.Error.CannotAttachAsModifiedWithoutOriginalState();
            }
            TrackedObject trackedObject = Context.Services.ChangeTracker.GetTrackedObject(entity);

            if (trackedObject == null || trackedObject.IsWeaklyTracked)
            {
                if (trackedObject == null)
                {
                    trackedObject = Context.Services.ChangeTracker.Track(entity, true);
                }
                if (asModified)
                {
                    trackedObject.ConvertToModified();
                }
                else
                {
                    trackedObject.ConvertToUnmodified();
                }
                if (Context.Services.InsertLookupCachedObject(inheritanceType, entity) != entity)
                {
                    throw new DuplicateKeyException(entity, System.Data.Linq.Strings.CantAddAlreadyExistingKey);
                }
                trackedObject.InitializeDeferredLoaders();
                return;
            }
            throw System.Data.Linq.Error.CannotAttachAlreadyExistingEntity();
        }