我的CustomContext如下所示。
public class CustomContext : DbContext
{
public CustomContext(DbConnection connection,DbCompiledmodel compiledmodel) : base(connection,compiledmodel,true)
{
Database.SetInitializer<CustomContext>(null);
}
//Other methods go here
}
我的服务注册如下所示
_container = new Container();
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
RegisterTypes(_container);
RegisterTypes方法
private static void RegisterTypes(Container container)
{
container.RegisterSingleton<IMapper>(() =>
{
var mapper = new Mapper(new MapperConfiguration(cfg =>
{
cfg.AddExpressionmapping();
}));
#if DEBUG
mapper.DefaultContext.ConfigurationProvider.AssertConfigurationIsValid();
#endif
return mapper;
});
container.Register(() => new AutoMapperConfiguration(),Lifestyle.Scoped);
container.Register<IUoWFactory,DbUoWFactory>(Lifestyle.Scoped);
container.Register<IRepository,DbRepository>(Lifestyle.Scoped);
container.Register<ICartonDetailService,CartonDetailService>(Lifestyle.Scoped);
var connectionString = Configurationmanager.AppSettings["DbConnection"];
container.Register<DbConnection>(() => new OracleConnection(connectionString),Lifestyle.Singleton);
container.Register<DbCompiledmodel>();
container.Register(() => new CustomContext(container.GetInstance<DbConnection>(),container.GetInstance<DbCompiledmodel>()),Lifestyle.Scoped);
container.Options.AllowOverridingRegistrations = true;
}
问题是DbCompiledmodel没有任何构造函数,因此我无法如上所述注册DbCompiledmodel和CustomContext。请帮我解决这个问题。
更新: 如下更改CustomContext的构造函数
ShamrockContext(DbConnection connection,Type[] types,ISfcInmemoryCache cache) : base(connection,cache.Getvalue<DbCompiledmodel>(types),false)
cache.Getvalue
通过从类型中读取“模型”列表来返回缓存的DbCompiledmodel。现在的问题是它在Type []上引发错误。有什么方法可以将运行时参数传递给这些构造函数。