ModelBuilder' does not contain a definition for 'ApplyAllConfigurations'?

asp.net-core dbcontext ef-fluent-api ef-model-builder entity-framework-core

Question

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?

1
0
1/4/2019 6:42:03 AM

Accepted Answer

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.

0
1/4/2019 6:44:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow