[英]VB.NET: Add DBContext with Dependecy Injection
I'm trying to consume in VB.NET some classes using Dependency Injection.我正在尝试使用依赖注入在 VB.NET 中使用一些类。
In the Global.asax.vb, I have:在 Global.asax.vb 中,我有:
.....
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.我在添加 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我想使用 Oracle 连接,但是当我尝试options.UseOracle(connString)
时,没有UseOracle扩展方法,但我添加了Oracle.EntityFrameworkCore nuget package
By using C# I simply use:通过使用 C# 我只需使用:
services.AddDbContext<MyDbContext>
(options => options.UseOracle(connString));
What is wrong?怎么了?
Thanks in advance.提前致谢。
Side note (so just community wiki), but recent VB.Net has implicit line continuation.旁注(只是社区 wiki),但最近的 VB.Net 有隐式的行延续。
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
I've resolved restarting Visual Studio (2022).我已决定重新启动 Visual Studio (2022)。 After the restart the extension method UseOracle was avaiable.重新启动后,扩展方法UseOracle可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.