When an exception is thrown from a custom convention the database is put into a bad state that causes the Initializer to not run.
Repro:
Create an console application following the standard blog/post/comment model (source code is attached)
Add a convention to the DbContext that throws an exception
e.g.
modelBuilder.Conventions.Add(
entities => entities
.Configure(c => c.ToTable(c.ClrType + "_table")));
run
change convention so it no longer throws an exception
modelBuilder.Conventions.Add(
entities => entities
.Configure(c => c.ToTable(c.ClrType.Name + "_table")));
run
note exception is thrown:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
expected behavior: Initializer should either drop the database or not create it in the case of an exception in the conventions
Repro:
Create an console application following the standard blog/post/comment model (source code is attached)
Add a convention to the DbContext that throws an exception
e.g.
modelBuilder.Conventions.Add(
entities => entities
.Configure(c => c.ToTable(c.ClrType + "_table")));
run
change convention so it no longer throws an exception
modelBuilder.Conventions.Add(
entities => entities
.Configure(c => c.ToTable(c.ClrType.Name + "_table")));
run
note exception is thrown:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
expected behavior: Initializer should either drop the database or not create it in the case of an exception in the conventions