是否有命令检查实体框架中是否存在数据库?

我可能措辞不好但在我使用的global.asx文件
if (System.Diagnostics.Debugger.IsAttached)
        {
            var test = new TestDbSeeder(App_Start.NinjectWebCommon.UcxDbContext);
            test.seed();
       }

这将检查调试器是否已连接并运行我的测试播种器,以便我的验收测试始终通过.

我需要检查数据库是否存在,如果没有先运行此代码

var test2 = new DataSeeder();
  test2.Seed(App_Start.NinjectWebCommon.UcxDbContext);

此数据处理器是必须始终位于数据库中的实际数据.是否有命令检查数据库是否存在,以便我可以运行该代码块.谢谢!

解决方法

Database.Exists方法适合您吗?
if (!dbContext.Database.Exists())
    dbContext.Database.Create();

编辑#1以回答评论

public class DatabaseBootstrapper
{
    private readonly MyContext context;

    public DatabaseBootstrapper(MyContext context)
    {
        this.context = context;
    }

    public void Configure()
    {
        if (context.Database.Exists())
            return;

        context.Database.Create();
        var seeder = new Seeder(context);
        seeder.SeedDatabase();
    }
}

这应该完全符合你的要求.在你的global.asax文件中……

public void Application_Start()
{
    var context = ...; // get your context somehow.
    new DatabaseBootstrapper(context).Configure();
}

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...