Skip to content

Instantly share code, notes, and snippets.

@merken
Created January 6, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merken/6570f3518cf498b95336563c9903a580 to your computer and use it in GitHub Desktop.
Save merken/6570f3518cf498b95336563c9903a580 to your computer and use it in GitHub Desktop.
using System.Data.Common;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Prise.Plugin;
using SQLPlugin.Configuration;
namespace SQLPlugin
{
[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("SQLPlugin", sqlConfig);
services.AddScoped<DbConnection>((serviceProvider) =>
{
// using Microsoft.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