ASP.NET Core InvalidOperationException: Unable to resolve service for type DbContext while attempting to activate UserStore

asp.net-core c# dbcontext entity-framework-core

Question

I have this DbContext in my Application which uses IdentityDbContext

public class ShopDbContext : IdentityDbContext<User>, IDisposable
    {


        public ShopDbContext(DbContextOptions options): base(options)
        {            
            Products = base.Set<Product>();
            Categories = base.Set<Category>();
            Shops = base.Set<Shop>();
            Properties = base.Set<Property>();
            Orders = base.Set<Order>();
            OrderItems = base.Set<OrderItem>();
            Customers = base.Set<Customer>();
            Carts = base.Set<Cart>();
            CartItems = base.Set<CartItem>();
            UserShops = base.Set<UserShop>();
            Images = base.Set<Image>();

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            // Use singular table names
            modelBuilder.ApplyConfiguration<Product>(new ProductConfiguration());
            modelBuilder.ApplyConfiguration<Category>(new CategoryConfiguration());
            modelBuilder.ApplyConfiguration<Shop>(new ShopConfiguration());
            modelBuilder.ApplyConfiguration<Property>(new PropertyConfiguration());
            modelBuilder.ApplyConfiguration<Order>(new OrderConfiguration());
            modelBuilder.ApplyConfiguration<OrderItem>(new OrderItemConfiguration());
            modelBuilder.ApplyConfiguration<Customer>(new CustomerConfiguration());
            modelBuilder.ApplyConfiguration<Cart>(new CartConfiguration());
            modelBuilder.ApplyConfiguration<CartItem>(new CartItemConfiguration());
            modelBuilder.ApplyConfiguration<UserShop>(new UserShopConfiguration());
            modelBuilder.ApplyConfiguration<Image>(new ImageConfiguration());
        }

        public DbSet<Shop> Shops { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Property> Properties { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderItem> OrderItems { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Cart> Carts { get; set; }
        public DbSet<CartItem> CartItems { get; set; }
        public DbSet<UserShop> UserShops { get; set; }
        public DbSet<Image> Images { get; set; }

    }

And this is my User model:

public class User : IdentityUser
    {
        public string Name { get; set; }
        public string LastName { get; set; }
        public string MobilePhone { get; set; }
        public string Address { get; set; }
        public Guid ImageId { get; set; }
        public Guid ShopId { get; set; }
        public DateTime LastActive { get; set; }
        public List<UserShop> UserShopList { get; set; }
        public bool HasImage()
        {
            return Guid.Empty != ImageId;
        }
    }

And the Startup code is like below:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    public IServiceProvider ServiceProvider { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContextFactory<ShopDbContext>(builder => 
            builder.UseSqlServer("DefaultConnection"));
        services.AddIdentity<User, IdentityRole>()
            .AddEntityFrameworkStores<ShopDbContext>()
            .AddDefaultTokenProviders();
        services.AddSqlServerDbContextFactory<ShopDbContext>();
        services.AddScoped<IShopEFRepository, EFRepository<ShopDbContext>>();

        Shop.Core.Configure.Configure.ConfigureServices(services);
        services.AddScoped<IUpdateService, UpdateService>();
        //services.AddSingleton<IBotService, BotService>();

        services.AddHttpContextAccessor();

        ServiceProvider = services.BuildServiceProvider();


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddCors();

        services.AddAuthentication(options => {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
                        .GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateLifetime = true,
                    ClockSkew = TimeSpan.Zero //the default for this setting is 5 minutes
                };
                options.Events = new JwtBearerEvents
                {
                    OnAuthenticationFailed = context =>
                    {
                        if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
                        {
                            context.Response.Headers.Add("Token-Expired", "true");
                        }
                        return Task.CompletedTask;
                    }
                };
            });

        services.AddAuthorization(options =>
        {
            options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin"));
            options.AddPolicy("User", policy => policy.RequireRole("User"));
            options.AddPolicy("ShopManager", policy => policy.RequireRole("Manager"));
            options.AddPolicy("DeviceRole", policy => policy.RequireRole("Device"));
            options.AddPolicy("OwnerRole", policy => policy.RequireRole("Owner"));
        });

        services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(opt =>
            {
                opt.SerializerSettings.ReferenceLoopHandling =
                    Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            });
        //services.AddCors();            
        Mapper.Reset();
        services.AddAutoMapper(typeof(Startup));


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
       // loggerFactory.AddMongoFramework<InfoLog>(ServiceProvider);
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler(builder =>
            {
                builder.Run(async context =>
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                    var error = context.Features.Get<IExceptionHandlerFeature>();
                    if (error != null)
                    {
                        context.Response.AddApplicationError(error.Error.Message);
                        await context.Response.WriteAsync(error.Error.Message);
                    }
                });
            });
        }

        app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
        app.UseAuthentication();
        app.UseMvc();
    }
}

Moreover I use EFCore.DbContextFactory for my generic repository. When I try to build the database I get exception:

InvalidOperationException: Unable to resolve service for type
        Shop.SQLData.ShopDbContext while attempting to activate
        Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore Shop.SqlModel.User,Microsoft.AspNetCore.Identity.IdentityRole,Shop.SQLData.ShopDbContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin`1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken`1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]].

I think the problem is in the way I set the DbContextFactory but As I followed the examples provided with it I did the same approach.

Besides, my connection string is:

"ConnectionStrings": {
    "DefaultConnection": "User ID=sa; Password=123456;Server=.\\sqlexpress;Database=ShopDb;"
  } 

When I try to generate the database using Package Manager update-database I get this error:

Format of the initialization string does not conform to specification starting at index 0.
1
0
8/19/2019 5:51:27 AM

Accepted Answer

 public ShopDbContext(DbContextOptions options): base(options)

try change it to this

 public ShopDbContext(DbContextOptions<ShopDbContext> options): base(options)

and change it

 services.AddDbContextFactory<ShopDbContext>(builder => 
            builder.UseSqlServer("DefaultConnection"));

to this

string connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContextFactory<DbContext,ShopDbContext>(builder => 
            builder.UseSqlServer(connectionString));
2
8/19/2019 5:46:11 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