How can i use Plugin DBContext ?
GrilleGustav opened this issue · comments
Hi, on the SmarthomePlugin.cs
` public class SmarthomePlugin : IPlugin
{
public SmarthomePlugin() { }
public string GetHref() => "/plugin/v2";
public IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddDbContext<RepositorySmarthomeContext>(opt => opt.UseMySql("Server=127.0.0.1;port=3306;database=api;uid=root;pwd=123;charset=latin1", new MySqlServerVersion(new System.Version(10, 5, 8))));
//services.BuildServiceProvider().GetService<RepositorySmarthomeContext>().Database.Migrate();
return services;
}
public void Configure(IApplicationBuilder appBuilder)
{
appBuilder.Map("/plugin/v1", c =>
{
var autoMapperType = typeof(AutoMapper.IMapper).Assembly;
c.Run(async (ctx) =>
{
await ctx.Response.WriteAsync("This plugin uses " + autoMapperType.GetName().ToString());
});
});
}
}`
i add DBContext of the plugin to create the plugin database tables.
Then i want use the controller with DbContext , but my api returns always "500 Internal Server Error".
I have only this. No erroor output on Kestrel console.
No error shown on debugging in Visual Studio.
My controller:
` [ApiController]
[Route("[controller]")]
public class SwitchController : ControllerBase
{
private readonly RepositorySmarthomeContext _context;
public SwitchController(RepositorySmarthomeContext context)
{
_context = context;
}
[HttpGet("[action]")]
public ActionResult GetVersion()
{
return Ok("0.0.1");
}
}`
How can i use DbContext in Plugins?
Ok I figured it out, they have a common project and there is a static reference from both the base application and the plugin.
For plugin services, you have registered the service interfaces in the shared project.
This is not a clean solution for me. I want to have the service class and interface only in my plugins and load them on application startup.
What can I do to change this?
instead of creating different db context for each plugin, the standard approach should be keeping a single dbcontext and loading your plugin entities use modelbuilder configs, ef and your database pool will thank you.