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);
        }
        public virtual DbContext CreateDbContext <TDbContext>(string connectionString, IDbContextResolver dbContextResolver)
            where TDbContext : DbContext
        {
            var dbContext = dbContextResolver.Resolve <TDbContext>(connectionString);

            DbContexts.Add(dbContext);
            return(dbContext);
        }
Exemple #3
0
        public async Task <Response <Countries> > AddCountry(Guid Id, string Country, string IsoCode)
        {
            var listCountryIsoCodes = dbContexts.Countries.Select(c => c.IsoCode).ToList();
            var existingCountry     = await dbContexts.Countries.FirstOrDefaultAsync(c => c.IsoCode == IsoCode.ToString());

            if (existingCountry != null)
            {
                return(new Response <Countries>
                {
                    Message = "The country already exist",
                    Model = existingCountry,
                    Successful = false
                });
            }
            else if (listCountryIsoCodes.Count == 10)
            {
                return(new Response <Countries>
                {
                    Message = "The number of countries exceed the limit of 10",
                    Successful = false
                });
            }
            var newCountry = new Countries()
            {
                ID      = Id,
                Country = Country,
                IsoCode = IsoCode
            };

            dbContexts.Add(newCountry);
            dbContexts.SaveChanges();

            return(new Response <Countries>
            {
                Message = "Successful",
                Model = newCountry,
                Successful = true
            });
        }