I am out to sea with this.
I received this answer which was corrected here by Nkosi.. however whilst it indicated how to rewrite the factory I am not up to the task of implementing it.. I need some gaps filled in.
As I understand it, this is what I need to do..
define the interface:
public interface IContextFactory<TContext> where TContext : DbContext
{
TContext Create(string connectionString);
}
Create the factory:
public class ContextFactory<TContext> : IContextFactory<TContext>
where TContext : DbContext
{
public TContext Create(DbContextOptions<TContext> options)
{
return (TContext)Activator.CreateInstance(typeof(TContext), options);
}
}
register the factory as a singleton in startup.
This is what I did and I dont know if this is correct?
services.AddSingleton(typeof(IContextFactory<>), typeof(ContextFactory<>));
Next how the heck do I use this factory?
In the first answer, it was suggested I use this:
public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IEntityBase, new()
{
private JobsLedgerAPIContext _context;
public string ConnectionString { get; set; }
public EntityBaseRepository(IContextFactory<JobsLedgerAPIContext> factory)
{
_context = factory.CreateDbContext(ConnectionString);
}
public virtual IQueryable<T> GetAll()
{
return _context.Set<T>().AsQueryable();
}
public virtual int Count()
{
return _context.Set<T>().Count();
}
}
Nkosi identified that you cannot have the constraint "new()" and a parameterized constructor. He rewrote it. How do I change the above code to reflect the fact the factory now does not have a parameterized constructor but still take in the connection string given Nkosi's factory?
ClientRepository inherits the above.
public class ClientRepository : EntityBaseRepository<Client>, IClientRepository
{
private new JobsLedgerAPIContext _context;
public ClientRepository(JobsLedgerAPIContext context) : base(context)
{
_context = context;
}
Does this constructor need to be changed.
And finally how do I Inject the ClientRepository now that I have to supply a connection string?
LobsLedgerAPIContextdependency. What you need is to make that context dependent of some service providing the information about the current logged in user - similar toAppTenantfrom ASP.NET Core Multi-tenancy: Data Isolation with Entity FrameworkOnConfiguringjust like in the linked article.