For my surprise, using the CreateDatabaseIfNotExists context initializer, the line
context.Database.Initialize(true)
doesn't throw an exception if the schema does not match my code first schema.
Is there a way to validate if the current database matches our schema before, for instance, we try to access a entity, whose table doesn't exist on the database anymore, and an exception is thrown by EF?
Schema. The default schema that EF Core uses to create database objects is dbo . You can change this behaviour using the ModelBuilder 's HasDefaultSchema method: protected override void OnModelCreating(ModelBuilder modelBuilder)
HasDefaultSchema(ModelBuilder, String) Configures the default schema that database objects should be created in, if no schema is explicitly configured.
Starting with EF6 you can use the HasDefaultSchema method on DbModelBuilder to specify the database schema to use for all tables, stored procedures, etc. This default setting will be overridden for any objects that you explicitly configure a different schema for.
You can call CompatibleWithModel to determine if the database matches the model. If you set the parameter to true it will throw an exception if no model data is found in the database.
bool isCompatible = context.Database.CompatibleWithModel(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With