Comments (4)
We have a similar requirement - specifically a user logs into our asp.net mvc application and after authenticating them against a default database, user specific information determines which database they connect to the remainder of their session.
Things to consider when adding this functionality:
1. Would you want to prevent the connection string changing within a scope's lifetime?
e.g.
using (var dbContextScope1 = _dbContextScopeFactory.Create())
{
var dbContext1 = dbContextScope.DbContexts.Get<UserManagementDbContext>(connectionString1);
using (var dbContextScope2 = _dbContextScopeFactory.Create())
{
var dbContext2 = dbContextScope2.DbContexts.Get<UserManagementDbContext>(connectionString2);
}
}
For our application I suspect it's preferable to force the connection string to stay constant for a given scope's lifetime.
2. Do you want to pass in the connection string every time you need to get DbContext?
It might be better to inject some configuration into the DBContextScopeFactory.
from dbcontextscope.
Looking more closely at the code, there's no need for changes. In order to support a dynamic connection string you can do the following.
- Create a class
DynamicDbContextFactory
that implements the providedIDbContextFactory
interface. - In the
CreateDbContext
method do whatever custom logic you need to return your DbContext initialised with the appropriate connection string. - When creating your
DbContextScopeFactory
ensure thedbContextFactory
parameter of the constructor is set to an instance ofDynamicDbContextFactory
. I'll probably do this using my DI container. - To get a
dbContextScope
, just do the same as the examples e.g.var dbContextScope1 = _dbContextScopeFactory.Create()
- To get a
dbContext
, just do the same as the examples e.g.var dbContext = dbContextScope.DbContexts.Get<UserManagementDbContext>();
Things to note:
- The
DynamicDbContextFactory.CreateDbContext
will only be called the first time aUserManagementDbContext
is created within that scope. (see DbContextCollection.cs line 66) If the conditions that determine the connection string change after the firstUserManagementDbContext
is created but before the 2ndUserManagementDbContext
is created (within the same dbContextScope's lifetime), the 2ndUserManagementDbContext
will have the same connection string as the first one. This is a good thing. - You don't need to pass in a connection string from all the code that uses the a DbContext.
- I haven't tested any of this but it sounds like it should work. I hope to implement it soon.
Thanks to mehdime for a great library and designing it in a way that it is able to be extended easily.
from dbcontextscope.
@ssippe did you have a chance to implement this feature? If you did would be able to share the code and your findings? Thank you!
from dbcontextscope.
No I didn't. One reason is that it hasn't been a priority for our business to refactor this into our system. The other reason is that I'm wavering in whether DbContextScope is the right way to go. Options I would consider in future projects are a) DbContextScope b) something along these lines https://www.reddit.com/r/dotnet/comments/2rcawo/managing_dbcontext_the_right_way_with_entity/cnfivdg or c) ditch EF for a micro-ORM like Dapper.
from dbcontextscope.
Related Issues (20)
- Compatibility with Effort
- Use with EntityFramework Reverse POCO Code First Generator? HOT 1
- nested IDbContextFactory.CreateWithTransaction HOT 1
- Need way to disable disposing of DbContext when root DbContextScope is disposed HOT 3
- .net core HOT 2
- Unable to use a non-interfaced DbContext with DbContextScope HOT 2
- TDD with DbContextScope HOT 1
- Connect to database with a different user/connection string HOT 5
- Moving to .Net Standard 2.0 & EF 2.0 HOT 8
- Best practice for taking advantage of EF first-level caching in Domain Service Layer. HOT 5
- Working with Store Procedure and Transaction
- DbContextScope with EF Core !! HOT 6
- Dead link
- Problems when attaching new dbcontexts while performing savechanges
- The current type, EntityFramework.DbContextScope.Interfaces.IDbContextFactory, is an interface and cannot be constructed. Are you missing a type mapping?
- Suggestions: how to retrieve ID generated by DB after saveChanges() HOT 2
- how to use DbContextScope with Parallel.ForEach
- Satellite class to use the same Factory/Locator across application modules
- What to pass to IDbContextFactory argument in DbContextScope constructor? HOT 1
- Support of ASP.NET Core dependency injection out of the box
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbcontextscope.