3

我有几个AutoFac和IoC入门问题。然而,我们有一个工作中的应用程序,我从头开始,并且无法看到两者之间的差异。AutoFac DbContext问题 - 模型创建时无法使用

我正在通过一个简单的AJAX页面测试这个页面,该页面通过ServiceStack API调用服务层。当使用MockRepositories这个工作正常,所以我知道事情的一面正在工作。

但是,当我使用实体框架替换模拟,尽管所有注册似乎都是正确的并且正常工作,但我得到错误“在创建模型时无法使用上下文”。

我已包括下面我的代码:

public class SomeObject 
{ 
    public int Id { get; set; } 
} 



public class IoCExampleContext : DbContext, IIoCExampleContext 
{ 

    public IDbSet<SomeObject> SomeObjects { get; set; } 

    static IoCExampleContext() 
    { 
     Database.SetInitializer(new IoCExampleDatabaseInitilizer()); 
    } 

    public IoCExampleContext(string connectionStringName) 
     : base(connectionStringName) 
    { 
     Configuration.ProxyCreationEnabled = false; 
    } 

    public IoCExampleContext() 
     : this("name=IoCExample") 
    {} 


    public string ConnectionString 
    { 
     get { return Database.Connection.ConnectionString; } 
    } 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     BuildModels(modelBuilder); 
    } 

    private void BuildModels(DbModelBuilder builder) 
    { 
     var typeToUse = typeof(SomeObjectModelBuilder); 
     var namespaceToUse = typeToUse.Namespace; 

     var toReg = Assembly 
         .GetAssembly(typeToUse) 
         .GetTypes() 
         .Where(type => type.Namespace != null && type.Namespace.StartsWith(namespaceToUse)) 
         .Where(type => type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); 

     foreach (object configurationInstance in toReg.Select(Activator.CreateInstance)) 
     { 
      builder.Configurations.Add((dynamic)configurationInstance); 
     } 
    } 
} 



public class IoCExampleDatabaseInitilizer : CreateDatabaseIfNotExists<IoCExampleContext> 
{ 
    protected override void Seed(IoCExampleContext context) 
    { 
    } 
} 



public interface IRepository<TEntity> where TEntity : class 
{ 
    IQueryable<TEntity> GetQuery(); 
    IEnumerable<TEntity> GetAll(); 
    IEnumerable<TEntity> Where(Expression<Func<TEntity, bool>> predicate); 

    // ...Various "standard" CRUD calls 
} 



public class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : class 
{ 
    protected DbContext _context; 
    private readonly DbSet<TEntity> _dbSet; 

    public GenericRepository(DbContext context) 
    { 
     _context = context; 
     _dbSet = _context.Set<TEntity>(); 
    } 

    public IQueryable<TEntity> GetQuery() 
    { 
     return _dbSet; 
    } 

    public IEnumerable<TEntity> GetAll() 
    { 
     return GetQuery().AsEnumerable(); 
    } 

    public IEnumerable<TEntity> Where(Expression<Func<TEntity, bool>> predicate) 
    { 
     return GetQuery().Where(predicate); 
    } 

    // ...Various "standard" CRUD calls 

    public void Dispose() 
    { 
     OnDispose(true); 
    } 

    protected void OnDispose(bool disposing) 
    { 
     if (disposing) 
     { 
      if (_context != null) 
      { 
       _context.Dispose(); 
       _context = null; 
      } 
     } 
    } 
} 


public class DependencyBootstrapper 
{ 
    private ContainerBuilder _builder; 

    public IContainer Start() 
    { 
     _builder = new ContainerBuilder(); 
     _builder.RegisterFilterProvider(); 
     RegisterControllers(); 
     return _builder.Build(); 
    } 

    private void RegisterControllers() 
    { 
     RegisterAssembly(Assembly.GetExecutingAssembly()); 
     _builder.RegisterModelBinderProvider(); 

     RegisterPerLifetimeConnections(); 
     RegisterRepositories(); 
     RegisterServices(); 
    } 

    private void RegisterAssembly(Assembly assembly) 
    { 
     _builder.RegisterModelBinders(assembly); 
     _builder.RegisterControllers(assembly); 
    } 

    private void RegisterRepositories() 
    { 
     _builder.RegisterGeneric(typeof(GenericRepository<>)).As(typeof(IRepository<>)); 
     _builder.RegisterType<GenericRepository<SomeObject>>().As<IRepository<SomeObject>>(); 
     //... More registrations 
    } 

    private void RegisterServices() 
    { 
     _builder.RegisterType<SomeObjectService>().As<ISomeObjectService>(); 
     //... More registrations 
    } 

    private void RegisterPerLifetimeConnections() 
    { 
     const string connectionStringName = "IoCExample"; 
     _builder.RegisterType<IoCExampleContext>() 
      .As<DbContext>() 
      .WithParameter("connectionStringName", connectionStringName) 
      .InstancePerLifetimeScope(); 

     _builder.Register(c => new HttpContextWrapper(HttpContext.Current)) 
      .As<HttpContextBase>(); 
    } 
} 

我不知道这是否是相关的,但因为我们无法获得访问我们调用通过PreApplicationStartMethod.OnPreApplicationStart引导程序在Global.asax方法(这据我所知,与Application_Start几乎相同)。

有点令人担忧的是,当我在连接字符串上启用多个活动结果集时它会起作用 - 这会暗示我注册DbContext的方式不正确,并且它跨越多个上下文。

任何人都可以发现我要去哪里错了吗?

+0

我面临同样的问题。你找到任何出路了吗? – bhuvin 2013-05-22 09:02:33

回答

0

连接字符串是问题。确保你在web/app.comfig中正确设置了它。

相关问题