question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an overload of DbModelBuilder.Configurations.AddFromAssembly() that accepts a namespace parameter

See original GitHub issue

DbModelBuilder.Configurations.AddFromAssembly() adds all EntityTypeConfiguration<TEntityType> classes from the DbContext’s assembly.

There may be assemblies containing multiple DbContext types, each requiring just a selection of the configuration types in the assembly. These configuration must therefore be added individually, which is easy to forget when a new entity class is introduced.

It would be very convenient to have an overload DbModelBuilder.Configurations.AddFromAssembly(namespaceName). Or, more flexible, as proposed below:

DbModelBuilder.Configurations.AddFromAssembly(Assembly assembly, Func<Type,bool> filter)

Since DbModelBuilder.Configurations.Add requires a generic type instance it’s hard (or undoable) to do this by reflection because the object created by Activator doesn’t have the correct type at compile time. It requires internal EF code to solve this problem as developer.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
CZEMacLeodcommented, Oct 9, 2021

@GertArnold I created a few extension methods using reflection to get all model types, or EntityTypeConfiguration or ComplexTypeConfiguration from an assembly, and add them to DbModelBuilder; I had an optional Func<Type,bool> filter to allow custom filtering, and a couple of overloads that used string rootNameSpace to include or IEnumerable<Type> to exclude unwanted types. I would have to remove some custom dependencies but I could probably publish them if it would help you out. To my mind, an overload of DbModelBuilder.Configurations.AddFromAssembly(Assembly assembly, Func<Type,bool> filter) would be the most useful to add to the core code.

0reactions
ajcvickerscommented, Apr 25, 2022

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

modelBuilder.Configurations.AddFromAssembly in EF Core
Is there any reason to prefer that way of doing things to . modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); , assuming ...
Read more >
Adding Entity Framework Code-First Type Configs from a ...
A recent project I worked on needed to connect to two different SQL databases using Entity Framework (version 6, not that new EF...
Read more >
DbModelBuilder.Properties Method (System.Data.Entity)
Overloads. Properties(). Begins configuration of a lightweight convention that applies to all properties in the model. Properties< ...
Read more >
EntityTypeConfiguration<TEntityType> Class
Derived types can be configured via the overload of Map that configures a derived type or by using an EntityTypeConfiguration for the derived...
Read more >
entityframework Wiki Rss Feed
We look at how classes are structured, which members are public as well as the names of classes, members and parameters. Namespace. Move...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found