I am trying to create a new asp.net core web application using Razor pages. I want to add my db context in the startup.cs file, but I am using something called IDesign.
My DbContext entity is in a project that I am not allowed to reference. I need to somehow add my context either at the accessor layer or something else. I'm not super familiar with how services work for .net core.
I have a solution set up like this:
My problem is that if my DbContext lives in the accessors project, how do I pass that up from the managers so that I can use it in the clients? Has anyone had experience with this before?
Just to reiterate, I know that I could easily reference accessors project in the clients project and use the dbcontext from there. My problem is that I want to avoid being able to reference accessors so that other people who are working with this code aren't able to see any accessors classes.
So I was able to accomplish this by following this guide: https://asp.net-hacker.rocks/2017/03/06/using-dependency-injection-in-multiple-projects.html
Instead of adding all of my services from the client, I was able to add them from the manager using an extension class and calling that extension method from the client.
Client Code:
public void ConfigureServices(IServiceCollection services)
{
services.AddManagerDependencyInjection();
services.AddGroupManagementDbContext();
// etc...
}
Manager Code:
public static class IServiceCollectionExtension
{
public static IServiceCollection AddManagerDependencyInjection(this IServiceCollection services)
{
services.AddTransient<IAccessor, Accessor>();
return services;
}
public static IServiceCollection AddDbContext(this IServiceCollection services)
{
var connection =
@"Server=(localdb)\mssqllocaldb;Database=DbName;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<MyContext>(
options =>
options.UseSqlServer(connection));
return services;
}
}