Entity Framework "Migration Configuration" and autofac

I'm trying to inject dependencies into the Entity Framework Configurations class and I can't seem to get it to work. The config class looks like this:

public class Configuration : DbMigrationsConfiguration<ApplicationContext>
{
    private readonly IApplicationUserManager _applicationUserManager;
    public Configuration(IApplicationUserManager userManager)
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        _applicationUserManager = userManager;
    }
    protected override void Seed(ApplicationContext context) {
      //seed data here
    }
}

      

And I am registering with Autofac like this -

  builder.Register(c => new Configuration(c.Resolve<ApplicationUserManager>()));

      

It won't work because running "Update-database" complains that "Migration Assembly Type was not found in assembly". So obviously he doesn't like the parameterless constructor. So I tried to use property injection like:

 public class Configuration : DbMigrationsConfiguration<ApplicationContext>
{
    public IApplicationUserManager ApplicationUserManager {get;set;}
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;

    }
    protected override void Seed(ApplicationContext context) {
      //seed data here
    }
}

      

and register it with Autofac like this -

builder.Register(c => new Configuration { ApplicationUserManager = c.Resolve<ApplicationUserManager>() });

      

However, this doesn't work either, the ApplicationUserManager property is always null. Is it possible to inject properties into a config class?

+3


source to share


2 answers


First of all, it looks weird that you want to use some dependency in the migration config class. So I advise you to think about aproach in general.

Explanation:

Your configuration is correct, but when you use dependency injection, you have to keep 2 things in mind:

  • you have to register your dependency in the dependency container.
  • you should create a component that contains the dependency using your dependency container, so this container will inject all dependencies during object creation.

So you've registered your dependency, but the problem is that the config class instance does not create a dependency container. If you instantiate through a dependency container eg.



DependencyResolver.Current.GetService<Configuration>();

      

your injection will work. In your situation, this is not acceptable because the configuration is generated by EF.

Possible solution: You can try to resolve the instance manually in your config class.

public class Configuration : DbMigrationsConfiguration<ApplicationContext>
{
    public Configuration(IApplicationUserManager userManager)
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }
    protected override void Seed(ApplicationContext context) {
       var applicationUserManager = DependencyResolver.Current.GetService<IApplicationUserManager>();
    }
}

      

Conclusion: Think about your aproach first, and if you really need to do this, please suggest a workaround.

+3


source


I think you can even use your User / Role / Claim classes without using dependency injection and just create objects in the Configuration class. This works for me, with Identity Framework.



    UserClaimService userClaimService = new UserClaimService(new EfRepository<UserClaim>(context));
    UserRoleService userRoleService = new UserRoleService(new EfRepository<UserRole>(context));
    UserLoginService userLoginService = new UserLoginService(new EfRepository<UserLogin>(context));
    RoleService roleService = new RoleService(new EfRepository<Role>(context));

    ApplicationUserService applicationUserService = new ApplicationUserService(
            new EfRepository<ApplicationUser>(context),
            userRoleService,
            roleService);

    UserManager<ApplicationUser, int> userManager = new UserManager<ApplicationUser, int>(new UserStore(applicationUserService, userClaimService, userRoleService, userLoginService, roleService));

      

0


source







All Articles