none
'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' RRS feed

  • 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...

    
    


    Fabricio Rodriguez - Pretoria, South Africa


    • Edited by Fabs1977 Monday, October 31, 2016 10:50 AM
    • Moved by CoolDadTx Monday, October 31, 2016 6:30 PM EF related
    Monday, October 31, 2016 10:41 AM

Answers

  • i solve this issue by adding

    using Microsoft.EntityFrameworkCore;


    humza tufail

    • Marked as answer by Fabs1977 Monday, October 30, 2017 7:17 AM
    Wednesday, May 24, 2017 2:32 PM

All replies

  • Try adding the following

    using Microsoft.Data.Entity;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by NBaghel Monday, March 23, 2020 12:56 PM
    Monday, October 31, 2016 10:53 AM
  • Hi Karen. Thanks for the quick reply. I did try adding using Microsoft.Data.Entity - but didn't make any difference.

    I also tried adding "using Microsoft.EntityFrameworkCore;" but that didn't help either. 


    Fabricio Rodriguez - Pretoria, South Africa

    Monday, October 31, 2016 11:13 AM
  • Hi Fabs1977,

    Based on your description, it seems that the sample, which is a sql server sample instead of MySQL sample.

    Please try to use Pomelo.EntityFrameworkCore.MySQL.

    Install-Package Pomelo.EntityFrameworkCore.MySQL

    Then use the following code to connection your database.

    services.AddDbContext<BloggingContext>(options => options.UseMySql(connection));

    For more information, please refer to:

    https://docs.efproject.net/en/latest/providers/pomelo/index.html

    https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/master/README.md#getting-started

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 1, 2016 3:24 AM
    Moderator
  • Thanks Cole - I had seen Pomelo, but I thought I'd go the official MySQL route. But since that hasn't been working for me, I will definitely try Pomelo. I'll let you know how it goes....

    Fabricio Rodriguez - Pretoria, South Africa

    Tuesday, November 1, 2016 8:04 AM
  • i solve this issue by adding

    using Microsoft.EntityFrameworkCore;


    humza tufail

    • Marked as answer by Fabs1977 Monday, October 30, 2017 7:17 AM
    Wednesday, May 24, 2017 2:32 PM
  • This was the solution that solved the problem for me as well. Thanks for posting!
    Monday, June 12, 2017 7:09 PM
  • Its a NuGet Packages Problem
    Install the following Packages and with its Proper Versions
    1 Microsoft.EntityFrameworkCore(Latest Version)
    2 Microsoft.EntityFrameworkCore.SqlServer(1.0.4 Version)
    • Edited by ParagPD Tuesday, July 4, 2017 7:23 AM
    Tuesday, July 4, 2017 7:23 AM
  • This did not work.
    Wednesday, June 13, 2018 1:36 PM
  •   <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.All" />
      </ItemGroup>

    in .proj file change Microsoft.AspNetCore.App to Microsoft.AspNetCore.All
    Friday, June 22, 2018 1:21 PM
  • thanks, it worked for me.
    Thursday, May 2, 2019 5:42 PM
  • You need to add the following to NuGet 

    Microsoft.EntityFrameworkCore.SqlServer.Design

    It will work for you. 

     
    Monday, September 9, 2019 7:28 AM