我是新的EF核心,我试图让它与我的ASP工作。NET Core项目。
当我试图配置DbContext
使用config中的连接字符串时,我在我的startup.cs
中得到了上述错误。我遵循本教程。
有问题的代码位于startup.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;
namespace tracV2
{
public class Startup
{
// 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.AddSingleton<IConfiguration>(Configuration);
string conn = Configuration.GetConnectionString("optimumDB");
services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
}
如果我将UseSqlServer
方法直接放入上下文中,它就会被识别:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace tracV2.data
{
public class tracContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("myrealconnectionstring");
}
我所有的在线研究都指向了缺失的参考资料,但我似乎找不到我缺失的是哪一个(见图片)。