0

We made an app in C#; we used EF but have created the models using a database-first approach. We lost the database and need to recreate it in order to continue development; how do I use the model in my database-first C# app in order to automatically create the database in SQL Server (trying to avoid tedious database and table re-creation).

Thanks in advance,

Ronan

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RCM
  • 19
  • 3
  • Probably a duplicate of https://stackoverflow.com/questions/38238043/how-and-where-to-call-database-ensurecreated-and-database-migrate - or at least the info you find there will help .. – Caius Jard Dec 15 '21 at 06:18
  • (Long story short, call `context.Database.EnsureCreated()` manually or in your app startup process somewhere) – Caius Jard Dec 15 '21 at 06:27
  • As a Db-first setup you also will probably have a line like this in the DbContext constructor: `Database.SetInitializer(null)` which you should comment out for that first run to recreate the DB. Note that the entities will likely not have enough information about the schema to fully recreate it when using Db First as commonly you won't have needed to worry about setting up attributes/settings for things like string column lengths, default constraints, or indexes that the original Db would already have had set up, but it should give you a head start on recreating the DB. – Steve Py Dec 15 '21 at 10:46

0 Answers0