-1

I haven't written a single line with EF Core yet but I have been reading tutorials and trying to get a grasp of the concept first. I have not been abl to find any answers to these questions regarding losing data accidentally:

  • Using Database First, if I wrote the database model by hand and forget to add some tables as DbSet objects in the DbContext, are the missing tables DROP'ed on SaveChanges?
  • If a DbSet is left empty and SaveChanges is called, will all the rows in the corresponding table be DELETE'd?

In general are there any pitfalls that could lead to data loss by mistake or if you forget to do something, or do all hard delete actions require explicit code?

hk1980
  • 95
  • 1
  • 7
  • Here is a link of how to create a your models for an existing database https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx – GoWiser Aug 29 '21 at 05:09

1 Answers1

0

I have reformulated the questions a little, to avoid misunderstandings.

Are the tables that aren't in my DbContext dropped on SaveChanges?

The tables will only be deleted if you drop the database or otherwise delete the tables.

If a DbSet is empty and SaveChanges is called, will the rows of the corresponding table be deleted?

Yes. Otherwise the framework wouldn't work.

Are there any pitfalls that could lead to data loss by mistake or if you forget to do something

It depends on what you mean by mistake or forget to do something. If you forget to save your changes, then your changes will be lost. If you, by mistake, modify, delete or drop tables, then of course your data will be lost.

You should always backup your database regularly, test that the backup can be restored, and test new things on a copy of the database.

GoWiser
  • 857
  • 6
  • 20