Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向SimpleInjector注册CustomContext(DbConnection连接,DbCompiledModel compiledModel)?_C#_Dependency Injection_Entity Framework 6_Simple Injector - Fatal编程技术网

C# 如何向SimpleInjector注册CustomContext(DbConnection连接,DbCompiledModel compiledModel)?

C# 如何向SimpleInjector注册CustomContext(DbConnection连接,DbCompiledModel compiledModel)?,c#,dependency-injection,entity-framework-6,simple-injector,C#,Dependency Injection,Entity Framework 6,Simple Injector,我的自定义上下文如下所示 public class CustomContext : DbContext { public CustomContext(DbConnection connection, DbCompiledModel compiledModel) : base(connection, compiledModel, true) { Database.SetInitializer<CustomContext>(null); }

我的自定义上下文如下所示

public class CustomContext : DbContext
{
    public CustomContext(DbConnection connection, DbCompiledModel compiledModel) : base(connection, compiledModel, true)
    {
        Database.SetInitializer<CustomContext>(null);
    }
    //Other methods go here
}
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;
}
私有静态无效注册表类型(容器)
{
container.RegisterSingleton(()=>
{
变量映射器=新映射器(新映射器配置(cfg=>
{
cfg.AddExpressionMapping();
}));
#如果调试
mapper.DefaultContext.ConfigurationProvider.AssertConfigurationsValid();
#恩迪夫
返回映射器;
});
container.Register(()=>new AutoMapperConfiguration(),
生活方式(范围);
容器。寄存器(生活方式。范围);
容器。寄存器(生活方式。范围);
容器。寄存器(生活方式。范围);
var connectionString=ConfigurationManager.AppSettings[“DbConnection”];
容器注册(()=>新OracleConnection(connectionString),
生活方式(单身);
container.Register();
container.Register(()=>new CustomContext(container.GetInstance(),container.GetInstance()),
生活方式(范围);
container.Options.AllowOverridingRegistrations=true;
}
问题是DbCompiledModel没有任何构造函数,因此我不能像上面那样注册DbCompiledModel和CustomContext。请帮我解决这个问题

更新: 更改了CustomContext的构造函数,如下所示

ShamrockContext(DbConnection connection, Type[] types, ISfcInMemoryCache cache) : base(connection, cache.GetValue<DbCompiledModel>(types), false)
ShamrockContext(DbConnection连接,类型[]类型,ISfcInMemoryCache缓存):基(连接,缓存.GetValue(类型),false)
cache.GetValue
通过从类型中读取模型列表,返回缓存的DbCompiledModel。现在的问题是它在类型[]上抛出错误。是否有一种方法可以将运行时参数传递给这些构造函数。

您有什么要求可以使用这个特定的DbContext构造函数?另一个构造函数可以吗?例如,接受连接字符串的服务?每个服务都知道要使用什么模型,这些类型将被编译、缓存并传递到DbContext。上下文将只能访问这些模型。为什么要使用singleton DbConnection。您知道连接不是线程安全的吗?您确定
类型
是运行时数据吗?这听起来像是在设计时就知道的东西?它们不是真正的运行时类型。每个服务都知道要使用什么模型。
ShamrockContext(DbConnection connection, Type[] types, ISfcInMemoryCache cache) : base(connection, cache.GetValue<DbCompiledModel>(types), false)