locked
Need usermanager under dbContext or any other class RRS feed

  • Question

  • User-1914871174 posted

    Hi there
    I need to use usermanager under dbcontext and I can't find a way to do it.

    I tried to inject it in the dbcontext constructor but I get  an error when I do that : "A circular dependency was detected for the service of type ..."

    here is whar I did:

    public class BlogDbContext : IdentityDbContext<IdentityUser>
        {
            private readonly IWebHostEnvironment _env;
            private readonly UserManager<BlogUser> _userManager;
    
            public BlogDbContext(DbContextOptions<BlogDbContext> options, 
                                IWebHostEnvironment env, 
                                UserManager<BlogUser> userManager)
                : base(options)
            {
                _env = env;
                _userManager = userManager;
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                // Apply properties and Seed Tables 
                modelBuilder.ApplyConfiguration(new CategoryConfiguration(_env.WebRootPath));
                modelBuilder.ApplyConfiguration(new PostConfiguration(_env.WebRootPath, _userManager));
            }
    
            public DbSet<Category> Categories { get; set; }
            public DbSet<Post> Posts { get; set; }
        }

    my ConfigureServices method looks like this :

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<BlogDbContext>(options =>
                    options.UseSqlite(
                        Configuration.GetConnectionString("BlogContext")));
                services.AddDatabaseDeveloperPageExceptionFilter();
    
                services.AddDefaultIdentity<BlogUser>(options => options.SignIn.RequireConfirmedAccount = false)
                    .AddEntityFrameworkStores<BlogDbContext>();
                services.AddControllersWithViews();
            }

    please any ideas ? 

    Sunday, April 18, 2021 9:37 PM

All replies

  • User475983607 posted

    The error is correct, the design creates an infinite circular reference because the DbContext is injected into the UserManager.

    Sunday, April 18, 2021 9:57 PM
  • User1120430333 posted

    You can't inject Usermanager into DBContext when Usermanger is already using DBcontext. Just what is it that your are trying to accomplish?

    Sunday, April 18, 2021 10:59 PM
  • User-1914871174 posted

    Hi

    I created a default user in AspNetUser table with name XXXXX.

    What I am trying to do is creating default entries in table 'Post' that has a FK property to userId.

    So I need to get the userId of the default user XXXXX by using usermanager.findbynameAsync function. that's I need the usermanager under dbContext.

    This is my code:

     public class PostConfiguration : IEntityTypeConfiguration<Post>
        {
    
            private readonly string _webRootPath;
            private readonly UserManager<BlogUser> _userManager;
    
            public PostConfiguration(string webRootPath, UserManager<BlogUser> userManager)
            {
                _webRootPath = webRootPath;
                _userManager = userManager;
            }
    
            public async void Configure(EntityTypeBuilder<Post> builder)
            {
                // Find Default User
                var user = await _userManager.FindByEmailAsync("XXXXX");
    
                if (user != null)
                {
                    // Seed data for this table
                    builder.HasData(
                        new Post
                        {
                            Id = 1,
                            BlogUserId = user.Id,
    .......

    the PostConfiguration  is called under OnModelCreating:

    modelBuilder.ApplyConfiguration(new PostConfiguration(....));

    thank you for your help.
      

    Sunday, April 18, 2021 11:28 PM
  • User475983607 posted

    Are you trying to seed data when the application starts?

    https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding

    djo_86

    So I need to get the userId of the default user XXXXX by using usermanager.findbynameAsync function. that's I need the usermanager under dbContext.

    This requirements is simply not possible due to DI fundamentals.

    The DbContext has direct  access to the User table.   If you need to create an account then you can use GetRequiredService() method to get the UserManager service.  Then create the account.

    https://andrewlock.net/access-services-inside-options-and-startup-using-configureoptions/

     

    Monday, April 19, 2021 10:54 AM