Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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 .NET Core.

I am following this tutorial. 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...

like image 768
Fabricio Rodriguez Avatar asked Oct 31 '16 12:10

Fabricio Rodriguez


3 Answers

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

like image 187
Joe Audette Avatar answered Nov 20 '22 21:11

Joe Audette


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.

like image 15
Frank Cannon Avatar answered Nov 20 '22 22:11

Frank Cannon


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

like image 8
HoganDevelopementAddicted Avatar answered Nov 20 '22 21:11

HoganDevelopementAddicted