c# - DbContextOptionsBuilder 不包含 'UseSqlServer' 的定义

标签 c# asp.net-core .net-core entity-framework-core

现在我想首先说明,我已经检查了所有关于该主题的非常相似的文章,但到目前为止没有任何问题可以解决我的问题。

我正在尝试为 .NET Core 设置 Entity Framework ,但当我尝试访问“UseSqlServer”方法时,我不断收到错误消息。根据我读过的其他文章,这实际上是 Microsoft.EntityFrameworkCore 中定义的扩展方法...我已经手动添加了对此的引用,可以确认它没有解决我的问题。

受影响的类很简单:

using ActivityService.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;

namespace ActivityService
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", false, true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // 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();

            // THIS IS WHERE I TRY TO USE THE METHOD
            services.AddDbContext<ActivityDbContext>(
                options => options.UseSqlServer(Configuration.GetConnectionString("Defaultconnection")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

我相信我已经安装了所有必需的软件包,所以我不确定我在这里缺少什么 :(

// ActivityService.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
  </ItemGroup>

</Project>

如果有人能对情况有所了解,我将不胜感激。

最佳答案

正确答案是您还必须在项目中包含 Microsoft.EntityFrameworkCore.SqlServer 包。

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

或者,你可以使用Nuget。

关于c# - DbContextOptionsBuilder 不包含 'UseSqlServer' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702132/

相关文章:

c# - 从数据库获取多个值 ASP.NET/C#

c# - 如何在浏览器控件中模拟点击事件? 。网

.net - ConfigureAwait(false) 与 ASP.NET Core 相关吗?

c# - 如何模拟输入异常的函数?

c# - Service Fabric 无状态服务器自定义 UDP 监听器

C# Xml - 名称字符无效;名称中不能包含 ' ' 字符

asp.net-core - ASP.NET Core 6 中的 appsettings.{Env}.json 层次结构

c# - 如何在asp.net-core webapi中使用unity-container拦截

c# - User.Claims 在 MVC 应用程序中为空

asp.net-core-mvc - GetExternalLoginInfoAsync 返回 null dotnet core 2.0