EFCore Not recognizing Database Provider

后端 未结 2 1824
广开言路
广开言路 2020-12-21 05:22

I have a .Net Core WebApplication Project in which the Context Class is in a Class Library. If I hard code the connection string in the OnConfiguring(DbContextOptionsBuilder

2条回答
  •  庸人自扰
    2020-12-21 06:28

    I was having the same issue when I when providing options along with dbcontext while adding dbcontext to services like:-

    services.AddDbContext(options => 
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    

    then I added dbcontext without options like below and it fixed the problem for me

    services.AddDbContext();
    

    also I had to add OnConfiguring method to dbcontext class so the context can access the connection string:-

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=IdenDB;Trusted_Connection=True;MultipleActiveResultSets=true");
    }
    

    I am not sure if this is the right way of doing things since I just started with core but here is an answer which explains this issue in a bit more detail

提交回复
热议问题