【问题标题】:DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver' in asp.net core 2.2?DbContextOptionsBuilder'不包含'usesqlserver'的定义,并且在asp.net core 2.2中没有扩展方法'usesqlserver'?
【发布时间】:2020-06-17 02:37:00
【问题描述】:

项目版本:.netcore2.2

我正在使用 .netcore2.2 中的 N 层架构

当我定义下面的配置然后给出错误:

DbContextOptionsBuilder' 不包含 'usesqlserver' 的定义,也没有扩展方法 '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!");
            });
        }
    }

程序.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>();
    }
}

见下图:

当我安装这个时:

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

当前错误:

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

我在上个中午试图解决这个错误,但错误没有解决

请帮忙?

【问题讨论】:

  • 来自this article,EF Core 2.2 是 ASP.NET Core 2.2 和新的 .NET Core SDK 的一部分。只要你安装了对应的Asp.net core SDK 2.2,使用UseSqlServer()时按Alt + Enter根据提示添加对应的程序集。
  • 并且 Packages of Dependencies 上有一个黄色警告三角形,这是一个明确的决定,向用户表明该项目的恢复尚未完成。

标签: asp.net-core


【解决方案1】:

安装nuget包:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

并将这个指令放在你的类中:

using Microsoft.EntityFrameworkCore;

没有指令或 nuget 包它不起作用

【讨论】:

    【解决方案2】:

    安装 nuget 包

    Install-Package Microsoft.EntityFrameworkCore.SqlServer
    

    【讨论】:

    • 我安装这个插件有什么问题请帮忙?
    • @Rahul 该错误告诉您确切的问题 - 使用正确的版本。试试这个:Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.2.6
    • @DavidG 再次报错Install-Package : NU1107: Version conflict detected for Microsoft.EntityFrameworkCore. Install/reference Microsoft.EntityFrameworkCore 3.1.2 directly to the project to resolve this issue. Microsoft.EntityFrameworkCore (&gt;= 3.1.2) DMEBase -&gt; Microsoft.AspNetCore.App 2.2.0 -&gt; Microsoft.EntityFrameworkCore (&gt;= 2.2.0 &amp;&amp; &lt; 2.3.0).
    • @DavidG 当我再次尝试这个时给出一个错误Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.2
    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 2017-11-25
    • 2017-03-13
    • 2021-08-03
    • 2017-08-05
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多