Home > Net >  Create SQL Database From DBContext EF Core
Create SQL Database From DBContext EF Core

Time:03-26

My pc suddenly got sick and all the data are removed and no way to recover them.

I was working on several projects with .NET 5 using EF core 6 Database first approach.

I cloned my projects from GitHub to get them back but unfortunately I don't have a backup or SQL file for the database.

Is there any way to create the database from the dbContext in the .NET 5 project?

CodePudding user response:

You should probably use dbContext.Database.EnsureCreated();

With this your DataBase structure will be re-created from the model. Of course, there will be no data, just empty tables, indexes etc.

CodePudding user response:

You can re-create the database structure with your existing model. But there is no way to recover the data

CodePudding user response:

I do not quite understand the question, is perhaps meant something like this?

//creates an instance of the class where the "database" is
Database db = new Database();
//creates the database if it does not exist
db.Database.EnsureCreated();
  • Related