錯誤訊息:
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<TContext> object in its constructor and passes it to the base constructor for DbContext.
原因:
透過AddDbContext進來的EFCore會發生optionsBuilder.IsConfigured永遠為true,造成無法運作,直到更新至2.1.11才被修正
過程:
Code-first加入方式Scaffold-DbContext
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<Models.Test.TContext>();
}
ValuesController.cs
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly Models.Test.TContext context;
public ValuesController(Models.Test.TContext context)
{
this.context = context;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
var tb = context.Table2.FirstOrDefault();
return new string[] { "value1", "value2" };
}
}
}
Context.cs
沒有留言:
張貼留言