Entity Framework Code First Relationship Mapping Problem
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)
{
modelBuild开发者_Python百科er.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!
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.
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.
精彩评论