c# - Entity Framework one context many databases -
in app, have 1 context user can select 1 of many databases during login. these databases based on same context , connection strings configured in web.config.
i have class caches user details:
public class userdetais { public string token { get; set; } public string connectionstring { get; set; } }
once user logs in, new userdetails object added cache later requests.
on receiving further requests, controller gets user's details cache , passes them on service, example:
public class customertypecontroller : apicontroller { private readonly icustomertypeservice customertypeservice; public customertypecontroller(icustomertypeservice customertypeservice) : base(logservice) { this.customertypeservice = customertypeservice; } protected override void initialize(httpcontrollercontext controllercontext) { base.initialize(controllercontext); var token = controllercontext.request.properties["token"] string; var details = usercache.get(token); customertypeservice.setuserdetails(details); } }
the service sets user details in unit of work, , @ stage, unit of work creates new context based on user's connection string.
this solution works, looks little hacky. there better way implement one-context-many-databases strategy?
Comments
Post a Comment