Code Monkey home page Code Monkey logo

Comments (5)

dusan-tkac avatar dusan-tkac commented on August 16, 2024

Can't you just create another DbContext using new connection string with same database with different credentials?
Set up your IDbContextFactory implementation to create new DbContext instance as well when requested and you get it's reference in the same way you're getting the reference to your current DbContext.

from dbcontextscope.

xr280xr avatar xr280xr commented on August 16, 2024

I'm using code first, but I could probably update the t4 file to generate another DbContext. That's an awful lot of duplicated code when all that needs to change is the connection to the database though. P.S. I don't currently have an IDbContextFactory but will look into it.

from dbcontextscope.

dusan-tkac avatar dusan-tkac commented on August 16, 2024

You don't have to generate it. Just hand-write the new DbContext in a separate project. Only include entities you need for that query.
Or you can use Entity Data Model Wizard's "Code First from Database" in the new project and again only include tables or views you need.

from dbcontextscope.

xr280xr avatar xr280xr commented on August 16, 2024

Sorry - above I meant I'm not using code first. I'm using database first. I have customizations to my DbContext that I would have to duplicate and keep in sync if I created another one. The DbContext is a model of my database and my database is the same database regardless of which user is using it. I don't see creating one DbContext per user as being a scalable or conceptually accurate solution.

For now, I modified my DbContextScope source. I created a CustomAmbientDbContextLocator whose Get method accepts a connection string parameter and changed the DbContextCollection's InitializedDbContexts to a Dictionary<KeyValuePair<Type, string>, DbContext>. So rather than my project having two different DbContexts, the DbContextCollection caches one instance of the DbContext per connection string it was created with. I think my implementation tarnishes the elegance of the project a little bit - it could be fancier - but it's doing what I need.

from dbcontextscope.

shoaibshakeel381 avatar shoaibshakeel381 commented on August 16, 2024

There is a much easier solution for your problem. You can just implement IDbContextFactory interface and provide DbContext instances yourself. This factory will be provided in IDbContextScopeFactory constructor. It is usually null and DbContextCollection creates DbContext object itself. But if a factory is available then that factory will be called for new instances.

You can easily use whichever connection string you want in that factory.

from dbcontextscope.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.