【问题标题】:Entity Framework Core: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'实体框架核心:DbContextOptionsBuilder 不包含“usesqlserver”的定义,也没有扩展方法“usesqlserver”
【发布时间】:2017-08-23 05:02:19
【问题描述】:

我是 EF Core 的新手,我正在尝试让它与我的 ASP.NET Core 项目一起使用。

当我尝试将DbContext 配置为使用配置中的连接字符串时,我在startup.cs 中收到上述错误。我正在关注this tutorial

有问题的代码在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");
        }

我所有的在线研究都指出缺少参考,但我似乎无法找出我缺少哪一个 (see image)。

【问题讨论】:

  • 同样的事情,intellisense 也找不到方法。

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


【解决方案1】:

我不得不使用这条线

 services.AddEntityFrameworkSqlite().AddDbContext<MovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));

在 Startup.cs 的 ConfigureServices 方法中

【讨论】:

    【解决方案2】:

    将以下代码从https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi 复制到 TodoApi.csproj 为我解决了类似的问题。

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <Folder Include="wwwroot\" />
      </ItemGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
      </ItemGroup>
    
      <ItemGroup>
        <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
      </ItemGroup>
    
    </Project>
    

    Microsoft.AspNetCore.All 可能过多但包含 EntityFrameworkCore

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但是在返回并修复 DbContext 不正确的语法问题(例如应该是 ExampleDbContextClass: DbContext)后问题就消失了,而我错过了定义 DbSet 的上下文类中的 DbContext 部分。此外,我验证了需要以下依赖项才能实现与 SqlServer 的连接。 &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" /&gt; &lt;PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" /&gt; 还要验证您安装的版本是否低于项目的当前版本以确保安全。例如,如果您的项目使用的是 3.1,请不要尝试使用较新的版本,例如 3.1.9。我也有这个问题。

      【讨论】:

        【解决方案4】:

        项目适用于 DotNET Core 3.1+ 或更高版本(未来)

        添加这个包:

        1. NuGet:Microsoft.EntityFrameworkCore.Tools
        2. 项目配置(.csproj):&lt;PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.8"&gt;

        【讨论】:

        • 这是不正确的。 UseSqlServer 是 Microsoft.EntityFrameworkCore.SqlServer 的一部分,如其他答案所述。
        【解决方案5】:

        我在包管理器控制台中安装了这三个,并且成功了。

        Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4 Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.8 Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.8

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-17
          • 2017-11-25
          • 2017-03-13
          • 2021-08-03
          • 2021-12-17
          • 2017-08-05
          • 1970-01-01
          相关资源
          最近更新 更多