简体   繁体   中英

ASP.NET core InvalidOperationException: 'No database provider has been configured for this DbContext./

I wanan save data on my app but i get

InvalidOperationException, 'No database provider has been configured for this DbContext.'

My code is below

public IActionResult Create(ProjectViewModel viewModel)
{
    ApplicationDbContext dbContext = new ApplicationDbContext(new Microsoft.EntityFrameworkCore.DbContextOptions<ApplicationDbContext>());
    dbContext.Project.Add(new Project()
    {
       ProjectName = viewModel.ProjectName,
       ProjectStart = viewModel.StartDate,
       ProjectEnd = viewModel.DeadLine,
       Priority = viewModel.Priority,
       Target = viewModel.Target,
       ProjectDescription = viewModel.Description,
       ProjectParts = viewModel.Amountofparts
    });
    dbContext.SaveChanges();
    return View(nameof(NewProject));
}

How can I fix it, any help would be appreciated.

需要将dbContext添加到服务中,如下所示:https: //docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?view = aspnetcore-2.1&tabs = visual-studio否则,您需要指定连接字符串在您的声明中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM