0

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?

1

5 Answers 5

3

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();
1
  • @gsharp It doesn't matter, anyway thanks that I can write comments now (have 59 points) To be honest, I hate this system. I also delete my comments and we act as if it never happened :)
    – Schecher_1
    Commented Mar 25, 2022 at 21:07
1

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.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 25, 2022 at 22:07
1

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

1

You can use this code in startup.cs file or program.cs '.net core 6'

`dbContext.Database.Migrate()`

This line will update database with all migrations which not applied on database

1

The fastest way was running these commands:

Add-Migration InitialCreate

and Then:

Update-Database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.