Entity Framework Core 2.0 Dynamically Creating A Database

entity-framework-core

Question

I am new to EF Core, and as I tried using it I found out that you need to add migrations for it to create a database from models. My question is, do we have another option aside from migrations to dynamically create the database on run time just like what it was in EF 6?

Thanks.

1
0
9/4/2017 8:18:35 AM

Popular Answer

Until a seeding mechanism is provided in EF Core you can provide your own seeding mechanism.

In earlier phases of a project, when the database is not yet fixed, I don't care that data get lost. When I want to recreate the database dynamically I call the function below. By the setting of a parameter I determine if this "recreateDatabase"-function is called yes or no. The function is included in MyOwnDbContext class.

The seed function you need to write is very similar to the one you use in EF 6.

    private static void recreateDatabase(YourOwnDbContext dbContext)
    {
        dbContext.Database.EnsureDeleted();
        dbContext.Database.EnsureCreated();

        seed(dbContext);
    }
0
9/4/2017 7:45:19 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow