示例#1
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);
            }
        }
示例#2
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);
        }