I'm using the "Create and Drop" APIs since my model changes often.
After a model change, I call dotnet ef database drop
, and on next run the system calls context.Database.EnsureCreated()
to recreate the database. I could precede that with context.Database.EnsureDeleted()
but I don't want to drop/recreate the database every time (it slows down the development "inner loop", and it'll hammer my drive).
There used to be an easy way (in old EF) to programmatically detect if the database and model are out of sync. There isn't such a built-in feature in EF Core. There are some ways (e.g., e.g.) but they are a few years old, unreliable (depend on internal features) and for older versions.
Is there a stable way - without using internal assembly features - to do this in v5?
context.Model
orcontext.Model.GetRelationalModel()
. Otherwise you have to serialise the model somewhere and useIMigrationsModelDiffer
, which creates the steps of a migration. Automatic migrations is a commonly requested feature (github.com/dotnet/efcore/issues/6214). – Mimi.EnsureCreated()
will create the scema if the database exists but has no tables. Not sure if dropping all tables would be quicker than dropping the database. – MimiExecuteSqlRaw
or whatever it's called). Then on next run compare, and if there are changes, callEnsureDeleted
first. Would probably work! Thanks for the valuable tip! – Slavoniccontext.Model.DebugView.LongView
– Mimicontext.ChangeTracker.DebugView.LongView
. Why do you believe it's the better option? – SlavonicDebugView
is only related to the objects that are currently being tracked. – Mimi