Topic:   Use multiple versions of dbcontext assembly in same application.
Mar 01, 2021 11:27 1 Replies 571 Views KRISHNA SWAROOP

There are different versions of application EF Core database context assembly in same application:

DbContext.dll
DbContextV1.dll
DbContextV2.dll
...
DbContextVn.dll

`DbContext.dll` is created statically at compile time.

Database spefic versions V1 .. Vn are created using Roslyn and can stored in application directory.

Invoking application using versioned url should use specific version of assembly:

https://example.com/V1 should use DbContextV1.dll

https://example.com/V2 should use DbContextV2.dll etc.

DbContext is registered in StartUp.cs as scoped service:

public void ConfigureServices(IServiceCollection services) {

 
services.AddHttpContextAccessor();

 
services.AddScoped();

 
.. 
}

So dependency injection creates new object instance on every request.

How to force DI to use request specific version of assembly for DbContext instance creation ?

Is it possible to create custom scoped service factory or use some other DI library ?

It looks like .NET 5 loads DbContext.dll on first use and does not allow to use different versions. This is ASP.NET 5 MVC Core application using EF Core with Npgsql data provider.

Prev Next
Topic Replies (1)
  1. 1
    idnkx user

    PARTH

    Instead of injecting a DbContext instance, you should inject a factory. The factory could be as simple as a dictionary of DbContext keyed by a version identifier.

    To build the factory you need dynamically load the versioned assemblies. For more information on the same, follow below link -

    https://docs.microsoft.com/en-us/dotnet/core/dependency-loading/loading-managed

    Note: unless your versions implemented a common interfaces you will need to use reflection to call the methods.

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect