0
votes

project version : .netcore2.2

I m working With N-Tier Architecture in .netcore2.2

when I defining below configuration then give the error:

DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'


    public class CustomerDataAccess : DbContext, ICustomerDataAccess
    {

        protected override void OnConfiguring(DbContextOptionsBuilder dbContextOptionsBuilder)
        {
            if (!dbContextOptionsBuilder.IsConfigured && _configuration.GetConnectionString("DMEBaseConnection") != null)
            {
                dbContextOptionsBuilder.UseSqlServer(_configuration.GetConnectionString("connectionstringname")); **//here give an error**
            }
        }

startup.cs

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }

program.cs

namespace projectname
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

see below image:

enter image description here

when I install this:

Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.6
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.2

Current Error:

NU1608: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.2.0 requires Microsoft.EntityFrameworkCore.SqlServer (>= 2.2.0 && < 2.3.0) but version Microsoft.EntityFrameworkCore.SqlServer 3.1.2 was resolved.

projectname-> projectname.BusinessLogic -> projectname.DataAccess -> Microsoft.EntityFrameworkCore (>= 3.1.2) 
 projectname-> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.EntityFrameworkCore (>= 2.2.0 && < 2.3.0).

I m trying to solve this error last midday but error not solved

plz, help?

2
From this article, EF Core 2.2 is as part of ASP.NET Core 2.2 and the new .NET Core SDK. As long as you have the corresponding Asp.net core SDK 2.2 installed, press Alt + Enter to add the corresponding assembly according to the prompts when using UseSqlServer().Xueli Chen
And there is a yellow warning triangle on the Packages of Dependencies , this is an explicit decision to indicate to the user that the restore has not yet completed for this project.Xueli Chen

2 Answers

3
votes

Install the nuget package

Install-Package Microsoft.EntityFrameworkCore.SqlServer
0
votes

Install the nuget package:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

And put this directive in your class:

using Microsoft.EntityFrameworkCore;

Without the directive or the nuget package it doesn't work