3
votes

Now I want to start by saying that I've already checked all of the very similar articles that have been posted on this topic and nothing has solved my issue so far.

I am trying to setup Entity Framework for .NET Core and I keep receiving an error when I try to access the 'UseSqlServer' method. According to the other articles that I have read, this is actually an extension method that is defined in Microsoft.EntityFrameworkCore...I have manually added references to this and can confirm that it has not resolved my issue.

The class that is affected is very simple:

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

I believe that I have all of the required packages installed so I'm not sure what I'm missing here :(

// 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>

If anyone can shed some light on the situation then I would greatly appreciate it.

1
Try put in using Microsoft.EntityFrameworkCore in your fileWebbanditten
I already tried doing that but unfortunately it didn't resolve the issue.spuriousGeek
accordingly to github.com/aspnet/EntityFramework/issues/7891 using should helpSet
Thanks for the link but I already read the article which is why I am kind of at a loss...I've tried several other solutions and nothing seems resolve the issue :( I even tried referencing older versions of the packages but that didn't help...if you have any ideas on what else I could check then that would be perfect.spuriousGeek
Your code doesn't show a using Microsoft.EntityFrameworkCore; statement. If you did in fact try that, I would go ahead and update the code in your question, because that the first thing that's going to pop up into everyone's mind.Alex Bello

1 Answers

13
votes

The correct answer is that you must also include the Microsoft.EntityFrameworkCore.SqlServer package in the project.

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Altern, you can use Nuget.