Entity Framework Core: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

前端 未结 30 3158
星月不相逢
星月不相逢 2020-12-13 03:14

I am new to EF core and I\'m trying to get it to work with my ASP.NET Core project.

I get the above error in my startup.cs when trying configure the

相关标签:
30条回答
  • 2020-12-13 03:55

    adding using Microsoft.EntityFrameworkCore;

    manually solved the problem for me

    Found that here

    Edit...

    for dotnet core 3.1 add

    Microsoft.EntityFrameworkCore.SqlServer

    0 讨论(0)
  • I had this issue, it seems that I hadn't added the required NuGet packages, although I thought I had done so, make sure to check them, one by one.

    0 讨论(0)
  • 2020-12-13 03:57

    First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package:

    PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer
    

    Then, after importing the namespace with

    using Microsoft.EntityFrameworkCore;
    

    we add the database context:

    services.AddDbContext<AspDbContext>(options =>
        options.UseSqlServer(config.GetConnectionString("optimumDB")));
    
    0 讨论(0)
  • 2020-12-13 03:58

    As mentioned by top scoring answer by Win you may need to install Microsoft.EntityFrameworkCore.SqlServer NuGet Package, but please note that this question is using asp.net core mvc. In the latest ASP.NET Core 2.1, MS have included what is called a metapackage called Microsoft.AspNetCore.App

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2

    You can see the reference to it if you right-click the ASP.NET Core MVC project in the solution explorer and select Edit Project File

    You should see this metapackage if ASP.NET core webapps the using statement

    <PackageReference Include="Microsoft.AspNetCore.App" />

    Microsoft.EntityFrameworkCore.SqlServer is included in this metapackage. So in your Startup.cs you may only need to add:

    using Microsoft.EntityFrameworkCore;

    0 讨论(0)
  • 2020-12-13 03:58

    For me this issue happened with Visual Studio Code and I was able to fix with 2 steps:

    1. Manually adding using Microsoft.EntityFrameworkCore;
    2. Running dotnet build in terminal.
    0 讨论(0)
  • 2020-12-13 03:58

    Copying the following code into the TodoApi.csproj from https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi solved similar issue for me.

    <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 may be excessive but it includes EntityFrameworkCore

    0 讨论(0)
提交回复
热议问题