问题描述
我创建了两个DbContexts,一个是应用程序的配置,第二个是记录。
I have created two DbContexts, one is for application configuration, the second is for logging.
其原因是,我想设置日志数据库的最大大小,因此不会从工作占用所有的可用磁盘空间和prevent其他数据库。
The reason being, I want to set a maximum size on the logging db so it doesn't use up all free disk space and prevent other databases from working.
在我的Global.asax.cs文件,我有以下几点:
In my global.asax.cs file, I have the following:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Database.SetInitializer<AdminContext>(new AdminInitialiser());
Database.SetInitializer<LoggingContext>(new LoggingInitialiser());
}
在LoggingInitialiser的InitializeDatabase方法不被调用。这是因为只有一个初始化可以设置?有什么办法让初始化两个DbContexts?
The InitializeDatabase method in LoggingInitialiser is not being called. Is this because only one initializer can be set? Is there any way to have initializers for two DbContexts?
推荐答案
设置初始化中的DbContext构造来代替。
Set the initializer in the DbContext constructor instead.
public class AdminContext : DbContext
{
public AdminContext()
{
Database.SetInitializer(new AdminInitialiser());
}
}
public class LoggingContext : DbContext
{
public LoggingContext()
{
Database.SetInitializer(new LoggingInitialiser());
}
}
这篇关于多的DbContext,多Database.SetInitializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!