gpt4 book ai didi

c# - 尝试创建初始迁移 EF Core 3 asp.net Core 时出错

转载 作者:行者123 更新时间:2023-12-02 16:51:48 25 4
gpt4 key购买 nike

我有以下类(class):

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; }

}

和我的 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);
}
}

当我尝试创建初始迁移时,出现以下错误:访问 Microsoft.Extensions.Hosting 服务时出错。在没有应用程序服务提供商的情况下继续。错误:成员的自定义配置仅支持类型的顶级个人成员。无法创建“ApplicationDbContext”类型的对象。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728 有人知道如何解决这个问题吗?谢谢:)

更新我的启动类:

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();
});
}
}

最佳答案

对我来说,卸载 docker-compose 是可行的。

清理和构建。

运行迁移。

enter image description here

关于c# - 尝试创建初始迁移 EF Core 3 asp.net Core 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58353418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com