Example #1
0
        internal TDbContext GetOrCreateDbContext <TDbContext>()
            where TDbContext : DbContext
        {
            var concreteDbContextType = _dbContextTypeMatcher.GetConcreteType(typeof(TDbContext));

            var connectionStringResolveArgs = new ConnectionStringResolveArgs(MultiTenancySides.Host);

            connectionStringResolveArgs["DbContextType"]         = typeof(TDbContext);
            connectionStringResolveArgs["DbContextConcreteType"] = concreteDbContextType;
            var connectionString = ResolveConnectionString(connectionStringResolveArgs);

            var dbContextKey = concreteDbContextType.FullName + "#" + connectionString;

            DbContext dbContext;

            if (!_activeDbContexts.TryGetValue(typeof(TDbContext), out dbContext))
            {
                if (Options.IsTransactional == true)
                {
                    dbContext = _transactionStrategy.CreateDbContext <TDbContext>(connectionString, _dbContextResolver);
                }
                else
                {
                    dbContext = CallContext.GetData("DbContext") as DbContext;
                    if (dbContext == null)
                    {
                        dbContext = Resolve <TDbContext>();
                        CallContext.SetData("DbContext", dbContext);
                    }
                }

                _activeDbContexts[typeof(TDbContext)] = dbContext;
            }

            return((TDbContext)dbContext);
        }
Example #2
0
 protected virtual string ResolveConnectionString(ConnectionStringResolveArgs args)
 {
     return(ConnectionStringResolver.GetNameOrConnectionString(args));
 }