gpt4 book ai didi

c# - AutoFac DbContext 问题 - 在创建模型时无法使用

转载 作者:行者123 更新时间:2023-11-30 18:35:07 27 4
gpt4 key购买 nike

我在开始使用 AutoFac 和 IoC 时遇到了一些问题。我们有一个可用的应用程序,但是我是从头开始使用这个应用程序的,看不出两者之间的区别。

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

然而,当我用使用 Entity Framework 的模拟替换模拟时,尽管所有注册看起来都是正确的并且有效,但我收到错误“创建模型时无法使用上下文。”

我在下面包含了我的代码:

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>();
}
}

我不知道它是否相关,但由于我们无法访问 global.asax 方法,我们通过 PreApplicationStartMethod.OnPreApplicationStart 调用 Bootstrap (据我所知,与 Application_Start 几乎相同)。

有点担心的是,当我在连接字符串上启用多个事件结果集时它起作用了——这向我暗示我注册的 DbContext 不正确并且它跨越多个上下文。

谁能发现我哪里出错了?

最佳答案

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

关于c# - AutoFac DbContext 问题 - 在创建模型时无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com