'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync

后端 未结 4 849
孤城傲影
孤城傲影 2020-12-08 18:11

.Net Core 1.0.0 - SDK Preview 2 (x64)

.Net Core 1.0.0 - VS "15" Preview 2 (x64)

.Net Core 1.0.0 - Runtime (x64)

So,

相关标签:
4条回答
  • 2020-12-08 18:36

    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.

    The error message says your DbContext(LogManagerContext ) needs a constructor which accepts a DbContextOptions. But i couldn't find such a constructor in your DbContext. So adding below constructor probably solves your problem.

        public LogManagerContext(DbContextOptions options) : base(options)
        {
        }
    

    Edit for comment

    If you don't register IHttpContextAccessor explicitly, use below code:

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
    
    0 讨论(0)
  • 2020-12-08 18:39

    I could resolve it by overriding Configuration in MyContext through adding connection string to the DbContextOptionsBuilder:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json")
                   .Build();
                var connectionString = configuration.GetConnectionString("DbCoreConnectionString");
                optionsBuilder.UseSqlServer(connectionString);
            }
        }
    
    0 讨论(0)
  • 2020-12-08 18:46

    Override constructor of DbContext Try this :-

    public DataContext(DbContextOptions<DataContext> option):base(option) {}
    
    0 讨论(0)
  • 2020-12-08 18:55

    This is the solution i found.

    https://github.com/aspnet/EntityFramework.Docs/blob/master/entity-framework/core/miscellaneous/configuring-dbcontext.md

    Configure DBContext via AddDbContext

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BloggingContext>(options => options.UseSqlite("Data Source=blog.db"));
    }
    

    Add new constructor to your DBContext class

    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
          :base(options)
        { }
    
        public DbSet<Blog> Blogs { get; set; }
    }
    

    Inject context to your controllers

    public class MyController
    {
        private readonly BloggingContext _context;
    
        public MyController(BloggingContext context)
        {
          _context = context;
        }
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题