C# 使用autofac注册多个dbcontext,其中生成器在注册后无法解析类
我有以下场景,依赖项注入发生在下面给出的类定义之外,因此我无法访问类中的builder对象。问题是DbContext被映射到两个类,因为我需要处理两个不同的数据库。为了创建SomeClass,需要实例化MyRepository和MyOtherRepository,但要做到这一点,需要两个不同的DBContext。这就是问题的症结所在,因为Autofac只接受DbContext映射到MyOtherContext的最后一个映射。非常感谢您为解决此问题提供的任何帮助,谢谢! 如果需要更多信息,请告诉我C# 使用autofac注册多个dbcontext,其中生成器在注册后无法解析类,c#,entity-framework,autofac,dbcontext,C#,Entity Framework,Autofac,Dbcontext,我有以下场景,依赖项注入发生在下面给出的类定义之外,因此我无法访问类中的builder对象。问题是DbContext被映射到两个类,因为我需要处理两个不同的数据库。为了创建SomeClass,需要实例化MyRepository和MyOtherRepository,但要做到这一点,需要两个不同的DBContext。这就是问题的症结所在,因为Autofac只接受DbContext映射到MyOtherContext的最后一个映射。非常感谢您为解决此问题提供的任何帮助,谢谢! 如果需要更多信息,请告诉我
public class DependencyResolver : IDependencyResolver
{
builder.RegisterType<MyContext>().As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<MyOtherContext>().As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<RepositoryManager.MyRepository>().As<Types.Repository.IMyRepository>().InstancePerDependency();
builder.RegisterType<RepositoryManager.MyOtherRepository>().As<Types.Repository.IMyOtherRepository>().InstancePerDependency();
}
试试这个。基本上,您正在寻找Autofac的功能。我更喜欢
因此,如果您注册这样的上下文:
var builder = new ContainerBuilder();
builder
.RegisterType<MyContext>()
.As<DbContext>()
.WithMetadata("EFContext", "My");
builder
.RegisterType<MyRepository>()
.AsImplementedInterfaces();
builder
.RegisterType<MyOtherContext>()
.As<DbContext>()
.WithMetadata("EFContext", "Other");
builder
.RegisterType<MyOtherRepository>()
.AsImplementedInterfaces();
public class SomeClass
{
private readonly IMyRepository _myRepository;
private readonly IMyOtherRepository _myOtherRepository;
public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
{
_myRepository = myRepository;
_myOtherRepository = myOtherRepository;
}
public string SayHello()
{
return $"Hello from {_myRepository.ContextName()} and {_myOtherRepository.ContextName()}";
}
}
public interface IMyOtherRepository
{
string ContextName();
}
public interface IMyRepository
{
string ContextName();
}
public class MyContext : DbContext
{
// Code
}
public class MyOtherContext : DbContext
{
// Code
}
public class MyRepository : IMyRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyRepository(IEnumerable<Meta<DbContext>> context)
{
_context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("My")).Value.GetType().Name;
}
public class MyOtherRepository : IMyOtherRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyOtherRepository(IEnumerable<Meta<DbContext>> context)
{
this._context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("Other")).Value.GetType().Name;
}
您应该能够将它们与非强类型元数据一起使用,如下所示:
var builder = new ContainerBuilder();
builder
.RegisterType<MyContext>()
.As<DbContext>()
.WithMetadata("EFContext", "My");
builder
.RegisterType<MyRepository>()
.AsImplementedInterfaces();
builder
.RegisterType<MyOtherContext>()
.As<DbContext>()
.WithMetadata("EFContext", "Other");
builder
.RegisterType<MyOtherRepository>()
.AsImplementedInterfaces();
public class SomeClass
{
private readonly IMyRepository _myRepository;
private readonly IMyOtherRepository _myOtherRepository;
public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
{
_myRepository = myRepository;
_myOtherRepository = myOtherRepository;
}
public string SayHello()
{
return $"Hello from {_myRepository.ContextName()} and {_myOtherRepository.ContextName()}";
}
}
public interface IMyOtherRepository
{
string ContextName();
}
public interface IMyRepository
{
string ContextName();
}
public class MyContext : DbContext
{
// Code
}
public class MyOtherContext : DbContext
{
// Code
}
public class MyRepository : IMyRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyRepository(IEnumerable<Meta<DbContext>> context)
{
_context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("My")).Value.GetType().Name;
}
public class MyOtherRepository : IMyOtherRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyOtherRepository(IEnumerable<Meta<DbContext>> context)
{
this._context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("Other")).Value.GetType().Name;
}
试试这个。基本上,您正在寻找Autofac的功能。我更喜欢
因此,如果您注册这样的上下文:
var builder = new ContainerBuilder();
builder
.RegisterType<MyContext>()
.As<DbContext>()
.WithMetadata("EFContext", "My");
builder
.RegisterType<MyRepository>()
.AsImplementedInterfaces();
builder
.RegisterType<MyOtherContext>()
.As<DbContext>()
.WithMetadata("EFContext", "Other");
builder
.RegisterType<MyOtherRepository>()
.AsImplementedInterfaces();
public class SomeClass
{
private readonly IMyRepository _myRepository;
private readonly IMyOtherRepository _myOtherRepository;
public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
{
_myRepository = myRepository;
_myOtherRepository = myOtherRepository;
}
public string SayHello()
{
return $"Hello from {_myRepository.ContextName()} and {_myOtherRepository.ContextName()}";
}
}
public interface IMyOtherRepository
{
string ContextName();
}
public interface IMyRepository
{
string ContextName();
}
public class MyContext : DbContext
{
// Code
}
public class MyOtherContext : DbContext
{
// Code
}
public class MyRepository : IMyRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyRepository(IEnumerable<Meta<DbContext>> context)
{
_context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("My")).Value.GetType().Name;
}
public class MyOtherRepository : IMyOtherRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyOtherRepository(IEnumerable<Meta<DbContext>> context)
{
this._context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("Other")).Value.GetType().Name;
}
您应该能够将它们与非强类型元数据一起使用,如下所示:
var builder = new ContainerBuilder();
builder
.RegisterType<MyContext>()
.As<DbContext>()
.WithMetadata("EFContext", "My");
builder
.RegisterType<MyRepository>()
.AsImplementedInterfaces();
builder
.RegisterType<MyOtherContext>()
.As<DbContext>()
.WithMetadata("EFContext", "Other");
builder
.RegisterType<MyOtherRepository>()
.AsImplementedInterfaces();
public class SomeClass
{
private readonly IMyRepository _myRepository;
private readonly IMyOtherRepository _myOtherRepository;
public SomeClass(IMyRepository myRepository, IMyOtherRepository myOtherRepository)
{
_myRepository = myRepository;
_myOtherRepository = myOtherRepository;
}
public string SayHello()
{
return $"Hello from {_myRepository.ContextName()} and {_myOtherRepository.ContextName()}";
}
}
public interface IMyOtherRepository
{
string ContextName();
}
public interface IMyRepository
{
string ContextName();
}
public class MyContext : DbContext
{
// Code
}
public class MyOtherContext : DbContext
{
// Code
}
public class MyRepository : IMyRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyRepository(IEnumerable<Meta<DbContext>> context)
{
_context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("My")).Value.GetType().Name;
}
public class MyOtherRepository : IMyOtherRepository
{
private readonly IEnumerable<Meta<DbContext>> _context;
public MyOtherRepository(IEnumerable<Meta<DbContext>> context)
{
this._context = context;
// Code
}
public string ContextName() => _context.First(x => x.Metadata["EFContext"].Equals("Other")).Value.GetType().Name;
}
感谢@Sakis的详细而及时的回复,成功了!太棒了,完成了!你介意接受它作为回答吗是的,我现在做了,很抱歉没有早点做,有点新鲜,再次感谢!感谢@Sakis的详细而及时的回复,成功了!太棒了,完成了!你介意接受它作为回答吗是的,我现在做了,很抱歉没有早点做,有点新鲜,再次感谢!