user7410216
user7410216

Reputation: 175

DropCreateDatabaseAlways in EntityFramework Core

I need to make a website for school using ASP.Net CORE along with MVC in Visual Studio. There is also a database connected to this website, which uses Entity Framework CORE. I have scaffolded quite a few models and the DbContext comes with it. The actual problem: I want the program to Drop the database and Create a new one using Entity Framework CORE every time the program is run.

I have found a lot articles about this same problem, but the solutions provided by them and the answers on other question by users on StackOverflow are not 'up to date' to the latest version of Entity Framework. In the older version, there was a neat solution to achieve my desired goal: simply use DropCreateDatabaseAlways and seed to immediately add data to the database. This option is gone in the new Core version (it isn't supported (yet)).

After searching for a long and painfull time, I couldn't find anything that works with this version of Entity Framework.

So I decided to ask you guys...! The scaffolded DbContext starts like this. I don't think more code of this class is actually needed, since that's al modelBuilder calls and DbSets being instantiated. But tell me if you need more.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

Any help would be very appreciated.

Sorry for bad English.

Upvotes: 2

Views: 6061

Answers (1)

bg_user
bg_user

Reputation: 41

Yes as you said DropCreateDatabaseAlways is EF 6. In EF Core I think you need to do the following:

context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Database.Migrate();

See more here: How and where to call Database.EnsureCreated and Database.Migrate?

Upvotes: 4

Related Questions