示例#1
0
        /// <summary>
        /// Returns the connection string for the specified database context
        /// </summary>
        /// <typeparam name="T">DbContext derived class</typeparam>
        /// <returns>Connection string associated with the DbContext</returns>
        public static string ConnectionString <T>() where T : class
        {
            IUserConfiguration config       = Initialize <T>();
            string             DatabaseName = typeof(T).Name.Replace("Context", "");

            return(config.ConnectionString(DatabaseName));
        }
        public static DbContextOptionsBuilder ConfigureSqlServer <T>(this DbContextOptionsBuilder optionsBuilder) where T : DbContext
        {
            if (!optionsBuilder.IsConfigured)
            {
                IUserConfiguration userConfiguration = ConfigFactory.Initialize <T>();
                string             DatabaseName      = typeof(T).Name.Replace("Context", "");
                optionsBuilder.UseSqlServer(userConfiguration.ConnectionString(DatabaseName));
            }

            return(optionsBuilder);
        }
        public static DbContextOptionsBuilder ConfigureSqlServer <T>(this T dbContext, DbContextOptionsBuilder optionsBuilder) where T : DbContext
        {
            object[] args = { };

            Type o = typeof(ConfigFactory);

            try
            {
                MethodInfo         mi                = o.GetMethod("GenericInitialize");
                MethodInfo         miConstructed     = mi.MakeGenericMethod(typeof(T));
                IUserConfiguration userConfiguration = (IUserConfiguration)miConstructed.Invoke(dbContext, args);

                string DatabaseName = dbContext.DBNameFromContext();
                optionsBuilder.UseSqlServer(userConfiguration.ConnectionString(DatabaseName));
            }
            catch (Exception e)
            {
            }

            return(optionsBuilder);
        }
        /// <summary>
        /// Creates a DbContextOptions object of type T
        /// </summary>
        /// <typeparam name="T">Type of DbContext to create options for</typeparam>
        /// <param name="DatabaseName">Name of the database whose connection string we are to use</param>
        /// <param name="UseLazyLoading">If true, lazy loading will be used</param>
        /// <returns>A live DbContextOptions of type T</returns>
        public static DbContextOptions <T> GetDbContextOptions <T>(string DatabaseName, bool UseLazyLoading = true) where T : DbContext
        {
            DbContextOptionsBuilder <T> optionsBuilder = new DbContextOptionsBuilder <T>();

            try
            {
                // The ConfigFactory static constructor reads the "MyProjectSettings" from appsettings.json
                // or secrets.json and exposes the IUserConfiguration interface. We use that interface
                // to retreive the connection string mapped to the DatabaseName.
                IUserConfiguration userConfiguration = ConfigFactory.Initialize <T>();
                string             ConnectionString  = userConfiguration?.ConnectionString(DatabaseName);

                if (UseLazyLoading)
                {
                    optionsBuilder.UseLazyLoadingProxies();
                }

                optionsBuilder.UseSqlServer(ConnectionString);
            }
            catch { }

            return(optionsBuilder.Options);
        }
        /// <summary>
        /// e.g. AddDbContextScoped SchoolContext
        /// </summary>
        /// <typeparam name="TImplementation"></typeparam>
        /// <param name="services"></param>
        /// <param name="UseLazyLoading"></param>
        public static void AddDbContextScoped <TImplementation>(this IServiceCollection services, bool UseLazyLoading = true) where TImplementation : DbContext
        {
            services.AddDbContext <TImplementation>(options =>
            {
                try
                {
                    // The ConfigFactory static constructor reads the "MyProjectSettings" from appsettings.json
                    // or secrets.json and exposes the IUserConfiguration interface. We use that interface
                    // to retrieve the connection string mapped to the DatabaseName.
                    IUserConfiguration userConfiguration = ConfigFactory.Initialize <TImplementation>();
                    string databaseName     = typeof(TImplementation).Name.Replace("Context", "");
                    string connectionString = userConfiguration?.ConnectionString(databaseName);

                    if (UseLazyLoading)
                    {
                        options.UseLazyLoadingProxies();
                    }

                    options.UseSqlServer(connectionString);
                }
                catch { }
            }, ServiceLifetime.Scoped);
        }