Home > database >  VB.NET: Add DBContext with Dependecy Injection
VB.NET: Add DBContext with Dependecy Injection

Time:01-27

I'm trying to consume in VB.NET some classes using Dependency Injection.

In the Global.asax.vb, I have:

.....
Dim hostBuilder As HostBuilder = New HostBuilder() _
            .ConfigureServices(Function(hostContext, services)
                                   services.AddLogging(Function(lb)
                                                           lb.ClearProviders()
                                                           lb.AddLog4Net("log4net.config")
                                                           Return lb
                                                       End Function)
                                   services.AddHttpClient(Of IService1, Service1)
                                   services.AddDbContext(Of MyDbContext)(Function(options)
                                                                           ' Not Working -> Return options.UseOracle(connString)
                                                                         End Function)
                                   services.AddScoped(Of IMyRepository, MyRepository)
                                   services.AddMediatR(GetType(SomeClass).GetTypeInfo().Assembly)
                                   services.AddScoped(Of IOtherClass, OtherClass)
                                   Return services
                               End Function) _
            .ConfigureAppConfiguration(Function(hostContext, confBuilder)
                                           confBuilder.AddJsonFile("appsettings.json", False)
                                           Return confBuilder
                                       End Function)

        Dim host = hostBuilder.Build()
        ServiceProvider = host.Services

I'm having problem in adding the DBContext.

I want to use an Oracle connection, but when i try options.UseOracle(connString), there isn't the UseOracle extension method, but I have added the Oracle.EntityFrameworkCore nuget package

By using C# I simply use:

services.AddDbContext<MyDbContext>
                (options => options.UseOracle(connString));

What is wrong?

Thanks in advance.

CodePudding user response:

Side note (so just community wiki), but recent VB.Net has implicit line continuation.

So if you have this:

 functionCall() _
    .ContinueExprssionOnNextLine

You can remove the _ if you move the . to the end of the first line, so there is a binary operator at the end of the line:

 functionCall().
    ContinueExprssionOnNextLine

CodePudding user response:

I've resolved restarting Visual Studio (2022). After the restart the extension method UseOracle was avaiable.

  • Related