简体   繁体   English

可以通过覆盖DbContext.OnConfiguring来配置提供程序

[英]A provider can be configured by overriding the DbContext.OnConfiguring

For some reason since I added a Application User class ,it says I have two contexts which I do not but I created the class as follows as it said to do so: 出于某种原因,因为我添加了一个Application User类,它说我有两个上下文,但我没有,但我按照以下方式创建了类:

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

But now it is saying the following: 但现在它说的如下:

No database provider has been configured for this DbContext. 没有为此DbContext配置数据库提供程序。 A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. 可以通过覆盖DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。 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. 如果使用AddDbContext,那么还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基础构造函数。

This is my db context layer: 这是我的数据库上下文层:

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");

            });

        }
    }

Anyone know what is up here? 谁知道这里有什么? I am using ASP.NET CORE 1.1. 我正在使用ASP.NET CORE 1.1。 Before I used my own Application user for Identy and this compiled fine. 之前我使用自己的应用程序用户为Identy和这编译好。 So I enclose it below in case something is wrong there. 因此,如果出现问题,请将其附在下面。

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

My startup.cs : 我的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?}");
        });


    }

My only guess is that now because I am using Application User Identity, user is making donet compiler thinking two contexts ? 我唯一的猜测是,因为我正在使用应用程序用户身份,用户正在编写donet编译器思考两个上下文?

I suggest you fixing this by adding IDesignTimeDbContextFactory implementation. 我建议你通过添加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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 - No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext 有什么方法可以检测何时从 Package 管理器控制台调用 DbContext.OnConfiguring()? - Any way to detect when DbContext.OnConfiguring() is being called from Package Manager Console? 在DbContext.OnConfiguring和AspCore Startup.ConfigureServices中都定义了optionsBuilder时,预期的结果是什么? - What are expected results when optionsBuilder is defined in both DbContext.OnConfiguring and AspCore Startup.ConfigureServices? 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” ASP.NET API:尚未为此DbContext配置任何数据库提供程序 - ASP.NET API: No database provider has been configured for this DbContext add-Migration 错误 没有为此 DbContext 配置数据库提供程序 - add-Migration Error No database provider has been configured for this DbContext 添加迁移错误:尚未为此DbContext配置数据库提供程序 - Add migration error: No database provider has been configured for this DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM