1

I am following this youtube video and at time 34:05, It is architecure for ASP.Net core using EntityFramework Core and they are showing an extension method

    modelBuilder.ApplyAllConfigurations();

I tried this in my code and it throws error: 'ModelBuilder' does not contain a definition for 'ApplyAllConfigurations' and no accessible extension method 'ApplyAllConfigurations' accepting a first argument of type 'ModelBuilder' could be found (are you missing a using directive or an assembly reference?

Am I missing any reference? If not, How can I implement this in my Project?

Ajay Kumar
  • 504
  • 2
  • 10
  • 23

1 Answers1

1

Check whether your ApplyAllConfigurations method as follows. I am using this and its working perfectly.

public static class ModelBuilderExtensions
{
    public static void ApplyAllConfigurations(this ModelBuilder modelBuilder)
    {
        var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces()
            .Any(gi => gi.IsGenericType && gi.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>))).ToList();

        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.ApplyConfiguration(configurationInstance);
        }
    }

}

Note: Ensure that this extension method and your DbContext are in the same Assembly. Other wise specify the assembly name explicitly in the extension method.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114