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

asp.net-core asp.net-mvc asp.net-web-api entity-framework mysql

Question

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

I am following this tutorial: https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework. However, I am connecting to a MySQL database instead of an SQL Server database - not sure how much difference that makes...

Anyway, in the tutorial, I have to "Register my context with dependency injection" - so I have to add the following line to the ConfigureServices section of the Startup.cs file:

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 no extension method 'UseSqlServer' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)

Any ideas why?

This is what the whole 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 have also added the MySQL package using the Package Manager, so that my project.json file contains this entry:

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

Any hints as to where I've gone wrong would be greatly appreciated, as I've spent all day trying to figure it out :( Thank you...

1
10
10/31/2016 12:23:53 PM

Accepted Answer

SqlServer is Microsoft Sql Server not MySql, to use SqlServer you would need the package "Microsoft.EntityFrameworkCore.SqlServer": "1.0.*".

If using MySql there is options.UseMySql

see this tutorial for more

30
10/31/2016 12:57:21 PM

Popular Answer

I got this error as well whilst setting up a project using Sql Server for a database. In my case I had to add a using statement manually - "using Microsoft.EntityFrameworkCore;" . It sounds a bit obvious but it threw me as Visual Studio wasn't adding the using statement via Quick Actions.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow