Пример #1
0
        public void BatchInsert(IEnumerable <TEntity> entities, int batchSize = 1000, Action <int> completePercentage = null)
        {
            var batcher = context.Database.Provider.GetService <IBatcherProvider>();

            if (batcher == null)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.NotSupportBatcher), null);
            }

            var syntax    = context.Database.Provider.GetService <ISyntaxProvider>();
            var rootType  = typeof(TEntity).GetRootType();
            var tableName = string.Empty;

            entities.ForEach(s => EntityPersistentSubscribePublisher.OnBeforeCreate(s));

            //if (Environment != null)
            {
                //    tableName = DbUtility.FormatByQuote(syntax, Environment.GetVariableTableName(rootType));
            }
            //else
            {
                var metadata = EntityMetadataUnity.GetEntityMetadata(rootType);
                tableName = DbUtility.FormatByQuote(syntax, metadata.TableName);
            }

            batcher.Insert(context.Database, entities, tableName, batchSize, completePercentage);
        }
Пример #2
0
        /// <summary>
        /// 将一个新的实体对象创建到库。
        /// </summary>
        /// <param name="entity">要创建的实体对象。</param>
        public void Create(IEntity entity)
        {
            ValidationUnity.Validate(entity);
            EntityPersistentSubscribePublisher.OnBeforeCreate(entity);

            try
            {
                Batch(() =>
                {
                    var isSucc = false;
                    entity.TryLockModifing(() =>
                    {
                        isSucc = InternalCreate(entity);
                        UpdateRelations(entity);
                    });
                    if (isSucc)
                    {
                        EntityPersistentSubscribePublisher.OnAfterCreate(entity);
                        entity.SetState(EntityState.Unchanged);
                    }
                });
            }
            catch (DbException exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityCreate), exp);
            }
        }
Пример #3
0
        public int Insert(TEntity entity)
        {
            EntityPersistentSubscribePublisher.OnBeforeCreate(entity);
            ValidationUnity.Validate(entity);

            var trans = CheckRelationHasModified(entity);

            if (trans)
            {
                context.Database.BeginTransaction();
            }

            int result = 0;

            try
            {
                if ((result = Queryable.CreateEntity(entity)) > 0)
                {
                    entity.As <IEntityPersistentEnvironment>(s => s.Environment        = context.Environment);
                    entity.As <IEntityPersistentInstanceContainer>(s => s.InstanceName = context.InstanceName);

                    HandleRelationProperties(entity);
                    EntityPersistentSubscribePublisher.OnAfterCreate(entity);
                }

                if (trans)
                {
                    context.Database.CommitTransaction();
                }
            }
            catch (Exception exp)
            {
                if (trans)
                {
                    context.Database.RollbackTransaction();
                }

                throw exp;
            }

            return(result);
        }