Is there a recommended way to seed a database on shared hosting with code first

208 views Asked by At

Every time I deploy and MVC site to a live host (usually shared hosting) I have a load of trouble seeding the database initially.

I keep trying various things and eventually it works, but it's not unusual to take a couple of hours of trial and error.

I create the DB on the shared host. Then I launch the app with CreateDatabaseIfNotExists. It often then gives me the unending 'loading' page.

I might run the app again, and perhaps it has created the DB tables, but not populated any of them.

Using any of the DropCreateDatabase options doesn't work - it doesn't have permission (presumably because on a shared host).

Rinse, lather, repeat, until finally it seems to work!

Is there a recommended way to create and seed the DB on a shared host? Do you have any recommendations for making this task easier?

Update

In Gobal.asax:

Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());

In the Initializer:

public class ApplicationDbInitializer : CreateDatabaseIfNotExists<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext db)
    {
        var dataSeeder = new DataSeeder(db);
        dataSeeder.SeedAll();
        base.Seed(db);
    }
}

Seeding is just standard code, like: db.Objects.AddRange(List<Object>)

0

There are 0 answers