0

In my C# application, I want to delete everything in System.Data.Entity.Databas when my application starts and re-populate my data from scratch programically.

I see there is an Initalizer method. System.Data.Entity.Database.SetInitializer()

How can I implement my initializer to delete everything from scratch ? and repopulate my data?

Thanks.

hap497
  • 154,439
  • 43
  • 83
  • 99
  • 2
    Look at [this SO Question](http://stackoverflow.com/questions/15220411/entity-framework-delete-all-rows-in-table) – Icemanind Sep 26 '14 at 21:23
  • Wait, you want to wipe out a table in your database and repopulate it when the application starts? Then what's the point of putting data in the table in the first place? Regardless, this is a very broad question. If you're using EF, clear the container and save changes. If not, create a stored procedure and call it when the program starts. – Arian Motamedi Sep 26 '14 at 21:26
  • Similar to [link](http://stackoverflow.com/questions/21491869/drop-or-recreate-database-with-entity-framework-migrations-code-first) – Hasan Fahim Sep 26 '14 at 21:27
  • Wouldn't be restore database from initial backup the simplest thing to do? I bet it will save you some time. – Gregor Primar Sep 26 '14 at 21:27

1 Answers1

1

If your schema is always the same (guess it is), why not a simple stored procedure which makes a Truncate on every table, or a simple list of plain text queries (started by ExecuteStoreCommand / ExecuteSqlCommand) to do the same Truncate at startup ?

Then you just have to repopulate, by the way you wish.

AFract
  • 8,868
  • 6
  • 48
  • 70