mehdime / DbContextScope

A simple and flexible way to manage your Entity Framework DbContext instances
http://mehdi.me/ambient-dbcontext-in-ef6/
MIT License
634 stars 271 forks source link

Same schema for many databases #32

Open mrbillo opened 8 years ago

mrbillo commented 8 years ago

We have many databases all exactly the same. We may open more than one context that are the same type. We use a generated connection string to accomplish this. Will this system know the difference between contexts when the type is the same but the connection is different ? Thanks

BloodBaz commented 8 years ago

The solution does allow you to specify your own connection string/connection name. With that facility you can write your own Connection String Generator class that will be able to take a standard connection string (from web.config) and replace the appropriate SQL Server Instance/Database Name on the fly. Worked for me.

mrbillo commented 8 years ago

I don't want to reuse the same context and change the database. I understand how to manage the connection string. The issue is: create context to database A create a context to database B. DbContextScope seems to match on context type which would be the same so it would return the context for database A instead of a new one. I would want to treat these separate so two context are created, each pointing to the correct database.

joepwijers commented 5 years ago

I see this is an old question, but for what it's worth: If you typically need simultaneous access to 2 of these databases (rather than a large number of parallel simultaneous connections) then you could consider to make 2 subclasses of your DbContext: DbContextA and DbContextB. It gives you 2 separate types.