コード例 #1
0
        private IDatabase CreateDatabase(ContextServiceContext context)
        {
            var factory = context.ServiceProvider.TryGetService <IDatabaseFactory>();

            if (factory != null)
            {
                return(factory.Create(context.Options));
            }

            IDatabase database;

            if (context.Options.Provider != null && context.Options.ConnectionString != null)
            {
                database = new Database(context.Options.ConnectionString, context.Options.Provider).TrySetServiceProvider(context.ServiceProvider);
            }
            else
            {
                throw new InvalidOperationException(SR.GetString(SRKind.NotSupportDatabaseFactory));
            }

            if (EntityTransactionScope.IsInTransaction())
            {
                database.BeginTransaction();
                EntityTransactionScope.Current.Addransaction((string)context.Options.ConnectionString, this);
            }

            return(database);
        }
コード例 #2
0
        /// <summary>
        /// 回滚数据库事务。
        /// </summary>
        public void RollbackTransaction()
        {
            if (EntityTransactionScope.IsInTransaction())
            {
                return;
            }

            Database.RollbackTransaction();
        }
コード例 #3
0
        /// <summary>
        /// 提交数据库事务。
        /// </summary>
        public void CommitTransaction()
        {
            if (EntityTransactionScope.IsInTransaction())
            {
                return;
            }

            Database.CommitTransaction();
        }
コード例 #4
0
        /// <summary>
        /// 释放资源。
        /// </summary>
        /// <param name="disposing"></param>
        protected override bool Dispose(bool disposing)
        {
            if (EntityTransactionScope.IsInTransaction())
            {
                return(false);
            }

            RollbackTransaction();

            if (Database != null)
            {
                Database.TryDispose(disposing);
                _database = null;
            }

            return(base.Dispose(disposing));
        }