'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'

I am trying to create a web API in VS 2015 Pro (Update 3) using C # and targeting with .NET Core.

I follow this guide: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework . However, I am connecting to the MySQL database instead of the SQL Server database - I don't know what the difference is ...

In any case, in the tutorial, I should "register my context using dependency injection" - so I need to add the following line to the ConfigureServices Startup.cs file section :

var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;

However, VS gives me the following error:

Error CS1061 "DbContextOptionsBuilder" does not contain a definition for "UseSqlServer", and the extension method "UseSqlServer" that accepts the first argument of the type "DbContextOptionsBuilder" cannot be found (do you miss the using directive or assembly reference?)

Any ideas why?

Here's what the entire Startup.cs file looks like:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace PropWorxAPI
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 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();

            var connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));
        }

        // 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 also added the MySQL package using the package manager, so this project.json file contains this entry:

*"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"*

Any hints about where I did wrong will be greatly appreciated, as I have been trying to figure this out all day :( Thanks ...

+4
source share
4 answers

SqlServer - Microsoft Sql Server, MySql, SqlServer "Microsoft.EntityFrameworkCore.SqlServer": "1.0. *".

MySql, options.UseMySql

.

+18

, Sql Server . using -    " Microsoft.EntityFrameworkCore;", , , Visual Studio using Quick Actions.

+11

If you have already installed the package "Microsoft.EntityFrameworkCore.SqlServer", and using Ctrl + Space cannot give you any option, adding "using Microsoft.EntityFrameworkCore" manually will solve my problem.

+3
source

From your project folder, enter this at the command line:

dotnet add package Pomelo.EntityFrameworkCore.MySql -v 2.1.2
0
source

Source: https://habr.com/ru/post/1659405/


All Articles