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();
Sign up to request clarification or add additional context in comments.

1 Comment

@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 :)
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 Comment

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.
1

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

Comments

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

Comments

1

The fastest way was running these commands:

Add-Migration InitialCreate

and Then:

Update-Database

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.