Entity Framework core: DbContextOptionsBuilder" ne contient pas de définition pour "usesqlserver" et aucune méthode d'extension "usesqlserver"

je suis nouveau à EF core et j'essaie de le faire fonctionner avec mon asp.net projet de base.

j'obtiens l'erreur ci-dessus dans mon startup.cs en essayant de configurer le dbcontext pour utiliser une chaîne de connexion de la configuration. Je suis ce tutoriel:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

le code problématique au démarrage.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));
        }

usesqlserver la méthode est reconnue si je la mets directement dans le contexte :

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");
        }

toutes mes recherches en ligne pointent vers des références manquantes, mais je n'arrive pas à trouver laquelle je manque(voir image).références

Toute aide serait grandement appréciée,

Merci

18
demandé sur marc_s 2017-03-29 18:37:31

10 réponses

C'est un problème connu dans le système de projet. Voir dotnet/projet-système n ° 1741

1
répondu bricelam 2017-03-31 16:47:17

nous installons Microsoft.Entiteframeworkcore.Paquet Nuget SqlServer.

Microsoft.Entiteframeworkcore.SqlServer

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

Ensuite,

   services.AddDbContext<AspDbContext>(options =>
       options.UseSqlServer(config.GetConnectionString("optimumDB")));
49
répondu Win 2017-03-29 15:47:25

ajout d' using Microsoft.EntityFrameworkCore;

manuellement résolu le problème pour moi

Trouvé qu'ici

30
répondu Victor.Uduak 2017-07-09 05:50:56

J'utilisais le code de Visual Studio.

1) Essayez D'installer le paquet 'Microsoft.Entiteframeworkcore.SqlServer ' en spécifiant le numéro de version.

VS Code:

'dotnet ajouter le package Microsoft.Entiteframeworkcore.SqlServer-V 1.1.1'

Visual Studio: -

'Install-Package Microsoft.Entiteframeworkcore.SqlServer-V 1.1.1'

Consulter le lien ' Package 'Microsoft.Entiteframeworkcore.SqlServer est incompatible avec tous les cadres du projet