可以通过重写DbContext.OnConfiguring来配置提供程序 [英] A provider can be configured by overriding the DbContext.OnConfiguring

查看:87
本文介绍了可以通过重写DbContext.OnConfiguring来配置提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:

尚未为此DbContext配置数据库提供程序.一种可以通过重写DbContext.OnConfiguring来配置提供程序方法或通过在应用程序服务提供程序上使用AddDbContext.如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受一个DbContextOptions对象,然后将其传递给DbContext的基本构造函数.

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.

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

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

            });

        }
    }

有人知道这是怎么回事吗?我正在使用ASP.NET CORE 1.1.在将我自己的Application用户用于Identy之前,这个编译还不错.因此,如果出现问题,我将其围在下面.

Anyone know what is up here? I am using ASP.NET CORE 1.1. Before I used my own Application user for Identy and this compiled fine. 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; }
}

我的 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编译器考虑两个上下文?

My only guess is that now because I am using Application User Identity, user is making donet compiler thinking two contexts ?

推荐答案

我建议您通过添加 IDesignTimeDbContextFactory 实现来解决此问题.

I suggest you fixing this by adding IDesignTimeDbContextFactory implementation.

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

这篇关于可以通过重写DbContext.OnConfiguring来配置提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆