Esempio n. 1
0
        /// <summary>
        /// 添加Configuration FreeSql实例到DI
        /// </summary>
        /// <param name="services"></param>
        /// <param name="storeOptionsAction">The store options action.</param>
        /// <returns></returns>
        public static IServiceCollection AddConfigurationDbContext(this IServiceCollection services,
                                                                   Action <ConfigurationStoreOptions> storeOptionsAction = null)
        {
            var options = new ConfigurationStoreOptions();

            services.AddSingleton(options);
            storeOptionsAction?.Invoke(options);

            var fsql = new FreeSql.FreeSqlBuilder()
                       .UseConnectionString(options.DataType, options.ConnectionString)
                       .UseAutoSyncStructure(true) //自动同步实体结构到数据库
                       .Build <ConfigurationDb>(); //请务必定义成 Singleton 单例模式

            fsql.ConfigureResourcesContext();
            fsql.ConfigureClientContext();
            fsql.SyncStructureResources();
            fsql.SyncStructureClient();

            services.AddSingleton(fsql);

            return(services);
        }