示例#1
0
        public TDbContext CreateDbContext()
        {
            var currentUow = _uowManager.GetCurrentUnitOfWork();

            if (currentUow == null)
            {
                throw new NullReferenceException("Cannot get a unit of work,Please check create root unit of work correctly");
            }

            var wantedDbContext = (TDbContext)currentUow.ServiceProvider.GetService(typeof(TDbContext));

            if (wantedDbContext == null)
            {
                throw new NullReferenceException("Cannot get DbContext.Please check add ef services correctly");
            }

            AddDbTransactionFeatureToUow(currentUow, wantedDbContext);

            return(wantedDbContext);
        }
示例#2
0
        public TDbContext CreateDbContext()
        {
            var currentUow = _uowManager.GetCurrentUnitOfWork();

            if (currentUow == null)
            {
                throw new NullReferenceException("Cannot get a unit of work,Please check create root unit of work correctly");
            }

            var transactionFeature = currentUow.GetTransactionFeature(_key);

            if (transactionFeature != null && transactionFeature is EFTransactionFeature efTransacationFeature)
            {
                var cacheDbContext = (TDbContext)efTransacationFeature.DbContext;
                if (!efTransacationFeature.IsOpenTransaction)
                {
                    //is need open transaction
                    OpenTransaction(efTransacationFeature, currentUow, cacheDbContext);
                }

                return(cacheDbContext);
            }

            var wantedDbContext = (TDbContext)currentUow.ServiceProvider.GetService(typeof(TDbContext));

            if (wantedDbContext == null)
            {
                throw new NullReferenceException("Cannot get DbContext.Please check add ef services correctly");
            }

            var newEFTransactionFeature = new EFTransactionFeature(wantedDbContext);

            currentUow.GetOrAddTransactionFeature(_key, newEFTransactionFeature);

            OpenTransaction(newEFTransactionFeature, currentUow, wantedDbContext);

            return(wantedDbContext);
        }