digipolisantwerp / dataaccess_aspnetcore_deprecated

Generic repository/unit of work framework for ASP.NET Core with Entity Framework.
Other
140 stars 45 forks source link

Using several connectionstring #40

Closed Kris-I closed 7 years ago

Kris-I commented 7 years ago

Hello,

First, I'd like to thank you for your great job.

In an Web application consuming a webApi, this WebApi use (should) your DAL. When a user try the consume it, they need to login first. I give a sample : companyA\user1 or companyB\user25 the connectionstring name are : companyA or companyB . Depending of this value, the context use a database or another, same schema but another database. Each company has is own database. Is there an easy way to give the connectionstring name for each request ?

Thanks,

StevenVandenBroeck commented 7 years ago

Hi @Kris-I,

thanks for the compliment :-)

What you are asking is currently not possible out-of the box with the current version of our library.

You could try the following, which is certainly not the best solution but an easy one without rework of the library :

services.AddSingleton<MyContext, MyContext>();
public IUnitOfWork CreateUnitOfWork<TContext>(bool trackChanges = true, bool enableLogging = false) where TContext : DbContext
{
   var context = (TContext)_serviceProvider.GetService(typeof(TContext));
   if (!trackChanges)
         context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
   var uow = new UnitOfWork(context, _serviceProvider);
   return uow;
}

I'm not sure this will work, but it is easy to try out.

Kris-I commented 7 years ago

I expected only one Context but change the connection string.

Thank for your answer,