我是新的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");
        }

我在网上的所有研究都指向了缺失的参考文献,但我似乎找不到我缺失的是哪一本(见图)。


当前回答

我也有同样的问题。我添加了以下内容。这对我很有用

Microsoft.EntityFrameworkCore.SqlServer

其他回答

我简单地解决了这个问题:

向相关类添加SqlServerDbContextOptionsExtensions 解决SqlServerDbContextOptionsExtensions

这修复了问题,必须在默认情况下缺少一些引用。

安装包,EntityFrameworkCore。:状态"置疑"

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.3

掘金: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/

解决这个问题的简单方法

错误信息:

解决方案: 安装“microsoft.entityframeworkcore”。sqlserver"与NuGet

修复:

PS:确保你在“using Microsoft.EntityFrameworkCore;”内容上使用了EF。

在Visual Studio中,检查NuGet Package Manager =>管理解决方案的包,检查所有这些包,无论是否安装在您的解决方案中,如下所示:

实体框架核心 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.InMemory Microsoft.EntityFrameworkCore.Relational Microsoft.EntityFrameworkCore.Sqlite.Core Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools

我解决了同样的问题后,检查所有上述软件包已安装。

包裹不见了。打开包管理器控制台,执行下面的代码:

Install-Package Microsoft.EntityFrameworkCore.SqlServer