【发布时间】: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