2
votes

I have the following classes:

public class Quiz 
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string UserToken { get; set; }
        public List<JoinQuizAndArea> AreasOfQuizzes { get; set; }
        public List<QuizQuestion> Questions { get; set; }
}
public class QuizQuestion
    {
        public int ListRanking { get; set; }
        public string Question { get; set; }
        public string Answer1 { get; set; }
        public string Answer2 { get; set; }
        public string Answer3 { get; set; }
        public int CorrectAnswer { get; set; }
        public int QuizId { get; set; }
        public Quiz Quiz { get; set; }
    }
public class AreaOfQuiz
    {
        public int Id { get; set; }
        public string Area { get; set; }
        public List<JoinQuizAndArea> AreasOfQuizzes { get; set; }
    }
public class JoinQuizAndArea
    {
        public int QuizId { get; set; }
        public Quiz Quiz { get; set; }
        public int AreaId { get; set; }
        public AreaOfQuiz Area { get; set; }

    }

and my DbContext:

public class ApplicationDbContext : IdentityDbContext<IdentityUser>
    {
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<Quiz> Quizzes { get; set; }
        public DbSet<AreaOfQuiz> Areas { get; set; }
        public DbSet<QuizQuestion> Questions { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityUser>();

            modelBuilder.Entity<Quiz>();

            modelBuilder.Entity<AreaOfQuiz>();

            modelBuilder.Entity<QuizQuestion>()
                .HasKey(quizQuestion => new { quizQuestion.QuizId, quizQuestion.ListRanking});

            modelBuilder.Entity<JoinQuizAndArea>()
                .HasKey(joinEntity => new { joinEntity.QuizId, joinEntity.AreaId });
            modelBuilder.Entity<JoinQuizAndArea>()
                .HasOne(join => join.Area)
                .WithMany(c =>c.AreasOfQuizzes)
                .HasForeignKey(join => join.QuizId);
            modelBuilder.Entity<JoinQuizAndArea>()
                .HasOne(bc => bc.Quiz)
                .WithMany(c => c.AreasOfQuizzes)
                .HasForeignKey(bc => bc.AreaId);

            base.OnModelCreating(modelBuilder);
        }
    }

when I try to create the initial migration I got the following error: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Custom configuration for members is only supported for top-level individual members on a type. Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 Do someone have an idea how to resolve this problem?? Thanks :)

Update My start Up class:

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

        public IConfiguration Configuration { get; }

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

            services.AddControllers();

            var mappingConfig = new MapperConfiguration(mapConfig =>
            {
                mapConfig.AddProfile(new QuizProfile());
                mapConfig.AddProfile(new AreaOfQuizProfile());
                mapConfig.AddProfile(new QuizQuestionProfile());
                mapConfig.AddProfile(new QuizIdAreaIdToJoinQuizAndAreaProfile());
            });
            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);

            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["ConnectionString:QuizWorldDb"]));

            services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(config =>{
                config.RequireHttpsMetadata=false;
                config.SaveToken = true;
                config.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Secret"])),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(builder => builder
                   .AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader());

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
1
The error points to your Startup class, not this code.Henk Holterman
@HenkHolterman, I added my startup class, thanks.HADDAD
I hope this will help you: stackoverflow.com/questions/35573006/…Viral Patel
@ViralPatel, thanks for the articales, indeed that was very help. well after reading and experiemnting with my code, I found some issues with the profiles in autoMapper. the above code is fine. Thank you again.HADDAD

1 Answers

6
votes

For me, unload docker-compose worked.

Clean and Build.

Run Migration.

enter image description here