-1

I wanted to create a many to many connection between the identity user and another table but when i modified my identity context and wanted to create a migration i got the following error

Unable to create a 'DbContext' of type 'IdentityContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[DataloaderApi.IdentityContext]' while attempting to activate 'DataloaderApi.IdentityContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

here are the codes that are maybe related to the issue:

dbcontext:

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using DataloaderApi.Data;

namespace DataloaderApi
{
    public class IdentityContext: IdentityDbContext<ApplicationUser>
    {

        public DbSet<TaskData> TaskData { get; set; }

       
        public IdentityContext(DbContextOptions<IdentityContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // Configuring many-to-many relationship
            builder.Entity<TaskData>()
                .HasMany(t => t.AssignedUsers)
                .WithMany(u => u.Tasks)
                .UsingEntity(j => j.ToTable("TaskDataUserJunction"));
        }

    }
}

the modified identity user

namespace DataloaderApi.Data
{
    public class ApplicationUser:IdentityUser
    {


        public ICollection<TaskData> Tasks { get; set; }

    }
}

TaskData:

using Microsoft.AspNetCore.Identity;

namespace DataloaderApi.Data
{
    public class TaskData
    {
        public long Id { get; set; }

        public string TaskName { get; set; }

        public string TaskDescription { get; set; }

        public string sourceLocation { get; set; }

        public string DestinationTable { get; set; }

        public bool isActive { get; set; }

        public ICollection<ApplicationUser> AssignedUsers { get; set; }



    }
}

And my program.cs


using System.Text;
using System.Threading.Tasks;
using DataloaderApi.Dao;
using DataloaderApi.Dao.Interfaces;
using DataloaderApi.Data;
using DataloaderApi.DataRead;
using DataloaderApi.Extension;
using Hangfire;
using IdentityAuthTest.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
namespace DataloaderApi
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerAuth();

            var connectionString = builder.Configuration.GetConnectionString("dataloaderConnection");

            builder.Services.AddHangfire(configuration => configuration      
                  .UseSqlServerStorage(connectionString)
                   
                  );


            builder.Services.AddDbContext<IdentityContext>(options =>
    options.UseSqlServer(connectionString));
            builder.Services.Configure<IdentityOptions>(options =>
            {
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 5;
                options.Password.RequireLowercase = true;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = false;
            });



            // Authentication 

            builder.Services.AddAuthorization();
            builder.Services.AddAuthentication();
            builder.Services.AddIdentityApiEndpoints<ApplicationUser>()
             .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<IdentityContext>();

            // Hangfire
            builder.Services.AddHangfireServer();
             builder.Services.AddDbContextPool<Applicationcontext>(options =>

                options.UseSqlServer(connectionString)
               

                );


            //Dependency Injection
            builder.Services.AddSingleton<IConfiguration>(builder.Configuration);

            builder.Services.AddScoped(typeof(ICsvLoadDao<>), typeof(CsvLoaderDao<>));
            builder.Services.AddScoped(typeof(IAuthHandlingDao), typeof(AuthHandlingDao));
            builder.Services.AddScoped<DataProcess>();
            builder.Services.AddAuthorization(options =>
            {
                options.AddPolicy("Admin", policy =>
                    policy.RequireRole("Admin"));
            });

            builder.Services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigins", policy =>
                    policy.WithOrigins("https://localhost:7046", "http://localhost:7046")
                          .AllowAnyMethod()
                          .AllowAnyHeader());
            });

            var app = builder.Build();

            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.MapIdentityApi<ApplicationUser>();
            app.UseCors("AllowSpecificOrigins");
            app.UseHttpsRedirection();

            app.UseAuthorization();
            app.UseHangfireDashboard();

            app.MapControllers();
            //Create admin user if no admin user exist
            using (var scope = app.Services.CreateScope())
            {
                await SeedData.Initialize(scope.ServiceProvider);
            }

            app.Run();
        }
    }
}

1 Answer 1

-1

I found the issue in my Dao i still referencing the identityUser in usermanager not my custom one

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.