Entity Framework core : DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

asp.net-core entity-framework-core

Question

I'm new to EF core and I'm trying to get it to work with my asp.net core project.

I get the above error in my startup.cs when trying confiugure the dbcontext to use a connection string from config. I'm following this tutorial : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

The problematic code in startup.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;

namespace tracV2
{
    public class Startup
    {
        // 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();

            services.AddSingleton<IConfiguration>(Configuration);

            string conn = Configuration.GetConnectionString("optimumDB");

            services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
        }

The usesqlserver method is recognized if I put it directly into the context :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace tracV2.data
{
    public class tracContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("myrealconnectionstring");
        }

All my reasearch online points to missing references, but I can't seem to find out which one i'm missing(see image).references

Any help would be greatly appreciated,

Thanks

1
122
5/4/2017 9:29:58 AM

Accepted Answer

This is a known issue in the project system. See dotnet/project-system#1741

14
3/31/2017 4:47:17 PM

Popular Answer

We install Microsoft.EntityFrameworkCore.SqlServer NuGet Package.

Microsoft.EntityFrameworkCore.SqlServer

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

Then,

   services.AddDbContext<AspDbContext>(options =>
       options.UseSqlServer(config.GetConnectionString("optimumDB")));

Update do not forget to add using Microsoft.EntityFrameworkCore; namespace



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