Skip to content

Instantly share code, notes, and snippets.

@merken
Created January 6, 2020 13:28
Show Gist options
  • Save merken/8c3d1be5b82db2ddbddaaf4a6a08f1db to your computer and use it in GitHub Desktop.
Save merken/8c3d1be5b82db2ddbddaaf4a6a08f1db to your computer and use it in GitHub Desktop.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OldSQLPlugin.Configuration;
using Prise.Plugin;
using System.Data.Common;
using System.Data.SqlClient;
namespace OldSQLPlugin
{
[PluginBootstrapper(PluginType = typeof(SqlProductsRepository))]
public class SqlPluginBootstrapper : IPluginBootstrapper
{
public IServiceCollection Bootstrap(IServiceCollection services)
{
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
var sqlConfig = new SQLPluginConfig();
config.Bind("OldSQLPlugin", sqlConfig);
services.AddScoped<DbConnection>((serviceProvider) =>
{
// using System.Data.SqlClient
var dbConnection = new SqlConnection(sqlConfig.ConnectionString);
dbConnection.Open();
return dbConnection;
});
services.AddScoped<DbContextOptions>((serviceProvider) =>
{
var dbConnection = serviceProvider.GetService<DbConnection>();
return new DbContextOptionsBuilder<ProductsDbContext>()
.UseSqlServer(dbConnection)
.Options;
});
services.AddScoped<ProductsDbContext>((serviceProvider) =>
{
var options = serviceProvider.GetService<DbContextOptions>();
var context = new ProductsDbContext(options);
return context;
});
return services;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment