9

I may have worded the question poorly but in my global.asx file i use

 if (System.Diagnostics.Debugger.IsAttached)
        {
            var test = new TestDbSeeder(App_Start.NinjectWebCommon.UcxDbContext);
            test.seed();
       }

This checks to see if the debugger is attached and runs my test seeder so that my acceptance tests always pass.

I need to check to see if the database exists as well and if not run this code first:

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

This dataseeder is the actual data that has to always be in the database. Is there a command to check if the database exists so that I can run that code block. Thanks!

Robert
  • 4,306
  • 11
  • 45
  • 95

2 Answers2

22

Will the Database.Exists method work for you?

if (!dbContext.Database.Exists())
    dbContext.Database.Create();

Edit #1 to answer comment

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

That should do exactly what you want. In your global.asax file...

public void Application_Start()
{
    var context = ...; // get your context somehow.
    new DatabaseBootstrapper(context).Configure();
}
Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
  • Maybe do you know where I can see examples of how that is used? I am googling around and have not found anything on it – Robert Nov 02 '12 at 16:06
  • I usually have a TestHelper class in my tests that can do things like stand up a database, an in-memory HTTP server, etc. So from my test fixture setup, I can call `TestHelper.CreateDatabase()`, which is exactly the code above. Then in my test fixture teardown, I call `TestHelper.DestroyDatabase()`. It's literally that easy. – Jarrett Meyer Nov 02 '12 at 16:09
  • 1
    I don't think that is quite what I am looking for. I was hoping to access something in the global.asx file that checked to see if the database exists and if it does not then run my seeder. If it does then skip the step – Robert Nov 02 '12 at 16:15
  • It's the same thing. Create new EF DBContext. Check if DB exists. If not create it and seed it. See forthcoming edit. – Jarrett Meyer Nov 02 '12 at 16:17
3

In Entity Framework Core it works like this:

namespace Database
{
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.EntityFrameworkCore.Storage;

    public partial class MyContextClass
    {
        /// <summary>
        /// Checks if database exists
        /// </summary>
        /// <returns></returns>
        public bool Exists()
        {
            return (this.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists();
        }
    }
}

Make sure the class name equals your database Context class name and is in the same namespace.

Use it like this:

var dbExists = (MyContextClass)db.Exists()

Source: StackOverflow Answer

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69