我创建了一个类,以便可以查询数据。如果在控制器类中执行此操作,则数据上下文有效,但如果在创建的类中执行此操作,则将引发null错误。它引发以下错误:
Microsoft.Extensions.DependencyInjection.Abstractions.dll,但未在用户代码中处理
我的启动配置:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<Abstractor_DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ApplicationDatabase")));
//services.AddTransient(typeof(Abstractor_DataContext));
//Register your filter as a service (Note this filter need not be an attribute as such)
services.AddTransient<AppExceptionFilterAttribute>();
services.AddMvc();
}
Run Code Online (Sandbox Code Playgroud)
我的类函数访问DataContext
using Microsoft.Extensions.DependencyInjection;
public static IList<ApplicationInfo> TestDb()
{
//var options = _serviceProvider.GetService<Abstractor_DataContext>();
using (var context = _serviceProvider.GetService<Abstractor_DataContext>())
{
return context.ApplicationInfo.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
var context一片空白。我想念什么?我正在慢慢学习DI。
我正在寻找自动拼写一个单词的方法,如果该单词拼写错误并且似乎是两个单词的组合。例如,“ considerationof”应为“ consideration of”。任何线索或任何示例将不胜感激。谢谢!