Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            DiscoverAssemblies();

            var efconfig = EntityFrameworkHelper.ReadConfiguration("config/EntityFramework.json");

            //services.AddDbContext<DataContext>(options => options.UseSqlite(connection));
            if (efconfig.SqlType == EntityFrameworkType.MsSql)
            {
                //services.AddEntityFrameworkSqlServer().
                //AddDbContext<MySqlDbContext>(options => options.UseSqlServer(efconfig.Connect))
                //  // .UseInternalServiceProvider(services)
                //  ;
                //var contextOptions1 = new DbContextOptionsBuilder<MySqlDbContext>()
                //.UseSqlServer(efconfig.Connect)
                //.Options;

                //services.AddSingleton(contextOptions1).AddScoped<MySqlDbContext>();


                services.AddEntityFrameworkSqlServer()
                .AddDbContext <CoreDbContext>((serviceProviders, options) =>
                                              options.UseSqlServer(efconfig.Connect,
                                                                   b => b.MigrationsAssembly("MyChy.Frame.Core.Web"))
                                              .UseInternalServiceProvider(serviceProviders));
            }
            services.AddSingleton <CoreDbContext>();
            services.AddSingleton <DbContext, CoreDbContext>();

            // Add framework services.
            services.AddMvc();

            var serviceProvider    = services.BuildServiceProvider();
            CoreEFStartupTask task = new CoreEFStartupTask(serviceProvider);

            task.RunS();
        }