Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if database schema matches Entity Framework schema?

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?

like image 713
Eduardo Brites Avatar asked Oct 26 '12 15:10

Eduardo Brites


People also ask

What is schema in entity Framework?

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)

Which of the following configures the default schema in entity Framework Core?

HasDefaultSchema(ModelBuilder, String) Configures the default schema that database objects should be created in, if no schema is explicitly configured.

How do I create a schema in entity Framework?

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.


1 Answers

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);
like image 53
AxdorphCoder Avatar answered Sep 30 '22 15:09

AxdorphCoder