Exemplo n.º 1
0
        /// <summary>
        /// Retrieves the current database (using Tenant Connectionstring)
        /// </summary>
        /// <typeparam name="T">The DbContext Type</typeparam>
        /// <param name="context">The Owin Context</param>
        /// <returns>A DbContext</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static T GetDbContext <T>(this IOwinContext context)
            where T : class
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var tenant = context.GetCurrentTenant();

            if (tenant == null)
            {
                return(Activator.CreateInstance <T>());
            }

            return((T)Activator.CreateInstance(typeof(T), tenant.ConnectionString));
        }