Ejemplo n.º 1
0
        public DbContext CreateDbContex <TDbContext>(string connectionString, IIocResolve iocResolve) where TDbContext : DbContext
        {
            DbContext dbContext;

            ActiveTransactionInfo activeTransaction;

            activeTransaction = ActiveTransations.TryGetValue(connectionString, out activeTransaction)
                ? activeTransaction
                : default(ActiveTransactionInfo);
            if (activeTransaction == null)//没找到则ioc通过NamedParameter创建一个
            {
                dbContext = iocResolve.ResolveParameter <TDbContext>(new NamedParameter("nameOrConnectionString",
                                                                                        connectionString));
                var dbTransaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
                activeTransaction = new ActiveTransactionInfo(dbTransaction, dbContext);
                ActiveTransations[connectionString] = activeTransaction;
            }
            else//找到了则通过ioc把DbConnection作为参数创建一个DbContext
            {
                dbContext = iocResolve.ResolveParameter <TDbContext>(new Parameter[] {
                    new NamedParameter("existingConnection", activeTransaction.DbContextTransaction.UnderlyingTransaction.Connection),
                    new NamedParameter("contextOwnsConnection", false)
                });                                                     //通过connection参数resolve一个dbcontext对象

                //dbContext = iocResolve.ResolveParameter<TDbContext>(new TypedParameter(typeof(DbConnection),
                //activeTransaction.DbContextTransaction.UnderlyingTransaction.Connection));
                dbContext.Database.UseTransaction(activeTransaction.DbContextTransaction.UnderlyingTransaction);
                activeTransaction.AttendedDbContexts.Add(dbContext);
            }
            DbContexts.Add(dbContext);
            return(dbContext);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Dispose
        /// </summary>
        /// <param name="iocResolve"></param>
        public virtual void Dispose(IIocResolve iocResolve)
        {
            foreach (var activeTransaction in ActiveTransations.Values)
            {
                foreach (var attendedDbContext in activeTransaction.AttendedDbContexts)
                {
                    iocResolve.Release(attendedDbContext);
                }
                activeTransaction.DbContextTransaction.Dispose();
                iocResolve.Release(activeTransaction.StartDbContext);
            }

            //清除事务
            ActiveTransations.Clear();

            foreach (var dbContext in DbContexts)
            {
                iocResolve.Release(dbContext);
            }
            //清除DbContexts
            DbContexts.Clear();

            if (CurrentTransaction == null)
            {
                return;
            }
            CurrentTransaction.Dispose();
            CurrentTransaction = null;
        }