【问题标题】:DbContextOptionsBuilder does not contain a definition for 'UseSqlServer'DbContextOptionsBuilder 不包含“UseSqlServer”的定义
【发布时间】:2017-11-25 21:28:01
【问题描述】:

现在我想说的是,我已经检查了所有发布在这个主题上的非常相似的文章,但到目前为止还没有解决我的问题。

我正在尝试为 .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>

如果有人能说明情况,我将不胜感激。

【问题讨论】:

  • 尝试在你的文件中输入using Microsoft.EntityFrameworkCore
  • 我已经尝试过这样做,但不幸的是它没有解决问题。
  • 感谢您的链接,但我已经阅读了这篇文章,这就是为什么我有点不知所措...我尝试了其他几种解决方案,但似乎没有任何解决问题的方法 :( 我什至尝试参考旧版本的软件包,但这没有帮助...如果您对我可以检查的其他内容有任何想法,那将是完美的。
  • 您的代码没有显示using Microsoft.EntityFrameworkCore; 语句。如果您确实尝试过,我会继续更新您问题中的代码,因为这是每个人都会想到的第一件事。

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


【解决方案1】:

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

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Altern,你可以使用 Nuget。

【讨论】:

  • 在我的情况下仍然失败。使用 v5.0.2
猜你喜欢
  • 2017-03-13
  • 2021-08-03
  • 2021-12-17
  • 2017-08-05
  • 1970-01-01
  • 2020-06-17
  • 2017-08-23
  • 1970-01-01
相关资源
最近更新 更多