mardi 6 février 2018

modelBuilder.Configurations.AddFromAssembly, or modelBuilder.Configurations.Add

I have an EF DBContext in the same assembly as my models and mappings (using database-first).

Previously, we have explicitly added each mapping into the context in the OnModelCreating method, but this is tedious and error-prone, if someone adds a table and mapping, but forgets to add it to the context in this way.

We would be writing something like this:
modelBuilder.Configurations.Add(new TableMap());

It seems that the alternative is to simply use something like the following:
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());

This seems to work fine, but my concern is that this must now be using reflection to get all the types that inherit from EntityTypeConfiguration<T>.

The EF context is being used by a RESTful web API, so my concern is that this code could run often if contexts are getting frequently created and destroyed by heavy API use.
I've been taught to avoid reflection if at all possible because of performance costs. Does this method of adding the entity mappings into the context come with any appreciable performance hit as a result of using reflection? If so, will the hit increase as the number of classes in the assembly goes up? Should I stick with explicitly adding all the entity configurations into the context?





Aucun commentaire:

Enregistrer un commentaire