如何在 Repository 中注入 EntityFramework Core DbContext
How to inject EntityFramework Core DbContext in Repository
大多数在线示例处理 asp.net 并将其 DbContext 注册为启动服务注册表的一部分。
我试过像这样注册我的 DbContext
builder.RegisterType<MyContext>()
.As<MyContext>()
.InstancePerLifetimeScope();
builder.RegisterType<DealRepository>()
.Keyed<IRepository<Deal>>(FiberModule.Key_DoNotSerialize)
.As<IRepository<Deal>>()
.SingleInstance();
builder.RegisterType<CardsDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
但是我遇到了这个错误
Inheritance security rules violated by type:
'System.Net.Http.WebRequestHandler'. Derived types must either match
the security accessibility of the base type or be less accessible.
它甚至更复杂,因为实际的 MessageController.cs
在 Post
上创建了一个新范围
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var dialog = scope.Resolve<IDialog<object>>();
await Conversation.SendAsync(activity, () => dialog);
}
如何注册?
编辑:
按照建议,使用 InstancePerRequest
解决了问题。但是我还有一个每 X 秒运行一次的 Quartz 作业,它也需要一个存储库。
builder.RegisterType<DealJob>()
.AsSelf()
.SingleInstance();
Unable to resolve the type 'BargainBot.Repositories.MyContext' because
the lifetime scope it belongs in can't be located. The following
services are exposed by this registration:
- BargainBot.Repositories.MyContext
Details ---> No scope with a tag matching 'AutofacWebRequest' is
visible from the scope in which the instance was requested. If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance()
此时我应该手动解析一个新的 DbContext 吗?或者也许我应该更改我的回购协议的生命周期?
Edit2:即使删除了整个 Quartz 作业注册,看起来我仍然遇到此错误。
我错了,这不是 IoC 和 DbContext 问题。似乎是在 .NET 平台本身
https://github.com/dotnet/corefx/issues/9846#issuecomment-274707732
添加重定向就成功了
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.TypeConverter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.0.0" />
</dependentAssembly>
正在安装 System.Net.Http 4.3.1 解决了我的问题
我遇到了类似的问题,经过多次来回,以下代码对我有用:
DbContext
public class SalesDbContext : DbContext
{
public SalesDbContext(DbContextOptions<SalesDbContext> options) : base(options)
{
}
public DbSet<Domain.Product.Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
// Configure database attributes
}
}
存储库
public class ProductRepository : IProductRepository
{
private readonly SalesDbContext _db;
public ProductRepository(SalesDbContext db)
{
_db = db;
}
}
Autofac 模块
public class DefaultModule : Module
{
protected override void Load(ContainerBuilder builder)
{
// Register Entity Framework
var dbContextOptionsBuilder = new DbContextOptionsBuilder<SalesDbContext>().UseSqlServer("MyConnectionString");
builder.RegisterType<SalesDbContext>()
.WithParameter("options", dbContextOptionsBuilder.Options)
.InstancePerLifetimeScope();
}
}
大多数在线示例处理 asp.net 并将其 DbContext 注册为启动服务注册表的一部分。
我试过像这样注册我的 DbContext
builder.RegisterType<MyContext>()
.As<MyContext>()
.InstancePerLifetimeScope();
builder.RegisterType<DealRepository>()
.Keyed<IRepository<Deal>>(FiberModule.Key_DoNotSerialize)
.As<IRepository<Deal>>()
.SingleInstance();
builder.RegisterType<CardsDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
但是我遇到了这个错误
Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.
它甚至更复杂,因为实际的 MessageController.cs
在 Post
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var dialog = scope.Resolve<IDialog<object>>();
await Conversation.SendAsync(activity, () => dialog);
}
如何注册?
编辑:
按照建议,使用 InstancePerRequest
解决了问题。但是我还有一个每 X 秒运行一次的 Quartz 作业,它也需要一个存储库。
builder.RegisterType<DealJob>()
.AsSelf()
.SingleInstance();
Unable to resolve the type 'BargainBot.Repositories.MyContext' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - BargainBot.Repositories.MyContext
Details ---> No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance()
此时我应该手动解析一个新的 DbContext 吗?或者也许我应该更改我的回购协议的生命周期?
Edit2:即使删除了整个 Quartz 作业注册,看起来我仍然遇到此错误。
我错了,这不是 IoC 和 DbContext 问题。似乎是在 .NET 平台本身
https://github.com/dotnet/corefx/issues/9846#issuecomment-274707732
添加重定向就成功了
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.TypeConverter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.0.0" />
</dependentAssembly>
正在安装 System.Net.Http 4.3.1 解决了我的问题
我遇到了类似的问题,经过多次来回,以下代码对我有用:
DbContext
public class SalesDbContext : DbContext
{
public SalesDbContext(DbContextOptions<SalesDbContext> options) : base(options)
{
}
public DbSet<Domain.Product.Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
// Configure database attributes
}
}
存储库
public class ProductRepository : IProductRepository
{
private readonly SalesDbContext _db;
public ProductRepository(SalesDbContext db)
{
_db = db;
}
}
Autofac 模块
public class DefaultModule : Module
{
protected override void Load(ContainerBuilder builder)
{
// Register Entity Framework
var dbContextOptionsBuilder = new DbContextOptionsBuilder<SalesDbContext>().UseSqlServer("MyConnectionString");
builder.RegisterType<SalesDbContext>()
.WithParameter("options", dbContextOptionsBuilder.Options)
.InstancePerLifetimeScope();
}
}