2

Can anybody tell me the real benefit of keeping Entity Framework model metadata in a database table (it does this using a default convention)?

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}

I can turn it off, and I think it makes the database cleaner to look at. But does this have some negative impact on performance?

I am using Code Only.

Thanks!

2 Answers 2

1

This will create EdmMetadata table which contains a hash of the current model. Every time you create an instance of your derived DbContext and you want to access the database, the context executes special query to get the stored hash. The context then compares the hash of the current model with the retrieved one. If the hash is different it either executes database intializer or fires exception. When using this feature you can for example define initializer:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyComtext>());

This initializer will drop the database and create new one if model changes. It makes code first development much simpler but it is something you mustn't use in production. You can also derive an initializer and create own one whith overriden Seed method. You can use Seed method to initialize your database.

Sign up to request clarification or add additional context in comments.

Comments

1

This is pretty important for the extensibility and maintainability of the application. It provides functionality for EF to notice schema changes and modify the database accordingly. Therefore allowing you to change your model code without having to worry about updating the database.

If you know for sure that you will never change the model's structure, then you can remove it.

1 Comment

Can you elaborate? I am using Code Only, and as far as I understood, any change to my Code Model would mean the database had to be manually ALTERed (or perhaps rebuild if there was no data in it) and that regardless of this setting EF would complain when the Code Model and Database Model were no longer in sync.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.