我创建了一个类,以便可以查询数据。如果在控制器类中执行此操作,则数据上下文有效,但如果在创建的类中执行此操作,则将引发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。
根据新的基于DB的ASP.net核心项目的官方文档,首先需要创建Models和DbContext:
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace EFGetStarted.AspNetCore.NewDb.Models
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后,使用依赖项注入注册您的上下文:
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;";
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
Run Code Online (Sandbox Code Playgroud)
然后使用数据库迁移创建数据库。现在,使用构造函数将上下文作为参数之一来创建控制器(BloggingContext context构造函数参数中的注释)。
using EFGetStarted.AspNetCore.NewDb.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace EFGetStarted.AspNetCore.NewDb.Controllers
{
public class BlogsController : Controller
{
private BloggingContext _context;
public BlogsController(BloggingContext context)
{
_context = context;
}
public IActionResult Index()
{
return View(_context.Blogs.ToList());
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Blog blog)
{
if (ModelState.IsValid)
{
_context.Blogs.Add(blog);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在任何类中使用DbContext都与在控制器中使用它相同。您将需要向依赖注入器注册您的课程。这是通过ConfigureServices方法完成的。官方文档的此页面包含详细信息。以下是一些注册服务类实现的示例:
services.AddScoped<ICharacterRepository, CharacterRepository>();
services.AddTransient<IOperationTransient, Operation>();
services.AddScoped<IOperationScoped, Operation>();
services.AddSingleton<IOperationSingleton, Operation>();
services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty));
services.AddTransient<OperationService, OperationService>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8631 次 |
| 最近记录: |