我首先在场景WPF MVVM EF代码中。 所有启动我的应用程序我必须初始化两个数据库,但每次丢失10秒才能完成所有操作。有办法加快吗?
private void InitDb()
{
var sednaComuniContext = new SednaComuniContext();
sednaComuniContext.Database.Initialize(false);//slowly
if (String.IsNullOrEmpty(Ditta) || Ditta == "BaseDb") return;
var sednaContext = new SednaContext(dbHelper.CreateConnectionString(Ditta));
sednaContext.Database.Initialize(false);//slowly
}
SednaComuniContext:
public class SednaComuniContext : DbContext,IContext
{
public static DatabaseHelper dbHelper;
static string _connection;
public SednaComuniContext()
: base(CreateConnectionString())
{
}
private static string CreateConnectionString()
{
dbHelper = new DatabaseHelper();
return dbHelper.CreateConnectionString("ArchiviComuni");
}
public SednaComuniContext(string connString)
: base(connString)
{
_connection = connString;
if (!Database.Exists())
{
Database.Initialize(true);
}
}
public DbSet<ArticoliFamiglia> ArticoliFamiglia { get; set; }
public DbSet<ArticoliGruppo> ArticoliGruppo { get; set; }
public DbSet<Articoli> Articoli { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SednaComuniContext, Configuration>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
SednaContext:
public class SednaContext : DbContext,IContext //IDbContext
{
public static DatabaseHelper dbHelper;
static string _connection;
public SednaContext()
: base(_connection)
{
}
public SednaContext(string connString)
: base(connString)
{
_connection = connString;
}
public DbSet<ArticoliFamiglia> ArticoliFamiglia { get; set; }
public DbSet<ArticoliFamigliaImpostazioni> ArticoliFamigliaImpostazioni { get; set; }
public DbSet<ArticoliGruppo> ArticoliGruppo { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SednaContext, Configuration>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
可以使用实体迁移吗?
答案 0 :(得分:1)
由于Entity Framework团队已将Entity Framework 6迁移到EntityFramework.dll,因此数据库的初始加载速度显着降低,其中一些问题已得到修复,但在控制台项目中构建具有2个实体的模型需要4-5对我来说也是一秒钟。请参阅此开放式项目,描述此问题的一部分:
http://entityframework.codeplex.com/workitem/2298
唯一的解决方法是在加载屏幕上向用户显示一些花哨的进度条,并显示加载此应用程序和应用程序的重要部分等术语。