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

时间:2017-07-29 17:58:01

标签: c# asp.net asp.net-core

由于某些原因,因为我添加了一个Application User类,它说我有两个上下文,但我没有,但我按照以下方式创建了类:

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

但是现在它说的是:

  

没有为此DbContext配置数据库提供程序。一个   可以通过覆盖DbContext.OnConfiguring来配置提供程序   方法或在应用程序服务提供程序上使用AddDbContext。   如果使用AddDbContext,那么还要确保您的DbContext类型   在其构造函数中接受DbContextOptions对象   将它传递给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。之前我使用自己的应用程序用户为Identy和这编译好。因此,如果出现问题,请将其附在下面。

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

1 个答案:

答案 0 :(得分:7)

我建议您通过添加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);
    }
}