There is currently no easy way to implement DropDatabseIfModelChangesin EFCore. EF6 worked by storing a snapshot of your model in a table __MigrationHistoryand comparing it with the current model. Such information is not stored in EnsureCreatedthe EFCore.
To emulate the behavior in EFCore, you can manually save the model hash whenever you create a database in EFCore, check the hash at startup, and delete and recreate the database if it has changed.
var currentHash = MyHashingFunction(db.Model);
if (db.GetService<IRelationalDatabaseCreator>().Exists()
&& !db.Set<ModelHash>().Any(mh => mh.Value == currentHash))
{
db.Database.EnsureDeleted();
}
if (db.Database.EnsureCreated())
{
db.Add(new ModelHash { Value = currentHash });
db.SaveChanges();
}
source
share