gpt4 book ai didi

c# - 可以通过覆盖 DbContext.OnConfiguring 来配置提供者

转载 作者:太空狗 更新时间:2023-10-29 20:19:29 24 4
gpt4 key购买 nike

出于某种原因,自从我添加了一个 Application User 类后,它说我有两个我没有的上下文,但我按如下所述创建了该类:

public class ApplicationDbContextFactory : IDbContextFactory<solitudeDContext>
{
public solitudeDContext Create(DbContextFactoryOptions options)
{
var optionsBuilder = new DbContextOptionsBuilder<solitudeDContext>();
return new solitudeDContext(optionsBuilder.Options);
}
}
}

但现在它说的是:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

这是我的数据库上下文层:

public class solitudeDContext : IdentityDbContext<IdentityUser>
{ public solitudeDContext(DbContextOptions<solitudeDContext> options) : base(options)
{
}
public DbSet<basketheader> BasketHeader { get; set; }
public DbSet<basketlines> BasketLines { get; set; }
public DbSet<customer> Customer { get; set; }

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

builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name: "AspNetUser", schema: "Security");
entity.Property(e => e.Id).HasColumnName("AspNetUserId");

});

}
}

有人知道这里有什么吗?我正在使用 ASP.NET CORE 1.1。在我使用我自己的应用程序用户作为身份之前,这个编译得很好。所以我将其附在下面以防出现问题。

public  class ApplicationUser: IdentityUser
{
public string FirstName { get; set; }
public string LastName{ get; set; }
public DateTime dob { get; set; }
}

我的startup.cs:

 // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddDbContext<IdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),b=>b.MigrationsAssembly("solitudeeccore")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<IMessageService, FileMessageService>();
services.AddAuthentication();
}

// 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.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});


}

我唯一的猜测是,现在因为我正在使用应用程序用户身份,所以用户正在让 donet 编译器考虑两个上下文?

最佳答案

我建议您通过添加 IDesignTimeDbContextFactory 实现来解决这个问题。

// TODO: Remove.
// (part of the workaround for https://github.com/aspnet/EntityFramework/issues/5320)
public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

var builder = new DbContextOptionsBuilder<ApplicationDbContext>();

var connectionString = configuration.GetConnectionString("DefaultConnection");

builder.UseSqlServer(connectionString);

// Stop client query evaluation
builder.ConfigureWarnings(w =>
w.Throw(RelationalEventId.QueryClientEvaluationWarning));

return new ApplicationDbContext(builder.Options);
}
}

关于c# - 可以通过覆盖 DbContext.OnConfiguring 来配置提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392363/

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