我是 EF Core 的新手,我正在尝试让它与我的 ASP.NET Core 项目一起使用。
尝试将 DbContext
配置为使用来自 config 的连接字符串时,我的 startup.cs
中出现上述错误。我正在关注 this tutorial。
有问题的代码在 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));
}
如果我将 UseSqlServer
方法直接放入上下文中,则可以识别它:
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");
}
我所有的在线研究都指向缺少参考文献,但我似乎无法找出我缺少哪一个 (see image)。
首先我们安装 Microsoft.EntityFrameworkCore.SqlServer NuGet 包:
PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer
然后,在导入命名空间后
using Microsoft.EntityFrameworkCore;
我们添加数据库上下文:
services.AddDbContext<AspDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("optimumDB")));
添加using Microsoft.EntityFrameworkCore;
手动为我解决了问题
编辑...
对于 dotnet core 3.1 添加
Microsoft.EntityFrameworkCore.SqlServer
在 NuGet Package 下面安装将解决您的问题
Microsoft.EntityFrameworkCore.SqlServer
安装包 Microsoft.EntityFrameworkCore.SqlServer
请按照以下步骤操作。
为 Entity Framework Core 安装 Entity Framework Core Design 和 SQL Server 数据库提供程序:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
导入实体框架核心:
using Microsoft.EntityFrameworkCore;
并配置您的 DbContext:
var connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(connectionString)
);
using
语句的解决方案的这一部分对我有用 - 似乎我的编辑器没有检测到程序集中的包
这是项目系统中的一个已知问题。请参阅dotnet/project-system#1741
包裹丢失。打开包管理器控制台并执行以下代码:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
您的解决方案效果很好。
当我看到这个视频直到 17 分钟:https://www.youtube.com/watch?v=fom80TujpYQ 我在这里遇到了一个问题:
services.AddDbContext<PaymentDetailContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
UseSqlServer 无法识别所以我做了这个 Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.5
& 使用 Microsoft.EntityFrameworkCore;
然后我的问题就解决了。关于我:基本上我从一开始就是一个纯粹的 PHP 程序员,今天我才开始 .net 编码,感谢 .net 中的良好社区
EntityFramework 使用SqlServer 已解决
安装包 Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 安装包 Microsoft.EntityFrameworkCore.SqlServer
我相信这可以通过添加对 Microsoft.EntityFrameworkCore.SqlServer.Design 的项目引用来解决
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.SqlServer 没有直接安装在我的项目中,但 .Design 包将作为先决条件安装它。
我正在使用 Visual Studio Code。
1) 尝试通过指定版本号来安装包“Microsoft.EntityFrameworkCore.SqlServer”。
VS代码:
'dotnet 添加包 Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
视觉工作室:-
'安装包 Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
请参阅链接“Package 'Microsoft.EntityFrameworkCore.SqlServer' is incompatible with 'all' frameworks in the project”进行操作。
2) 然后添加命名空间'using Microsoft.EntityFrameworkCore;'手动在 Startup.cs 文件中。
请参阅以下链接 https://github.com/aspnet/EntityFramework/issues/7891。
3) 如果您遇到“Microsoft.EntityFrameworkCore.SqlServer.Design”的任何依赖问题,例如“Package 'Microsoft.EntityFrameworkCore.Design' is incompatible with 'all' frameworks in project”,我们需要运行以下命令,
VS代码:-
dotnet 添加包 Microsoft.EntityFrameworkCore.Design -v 1.1
视觉工作室
安装包 Microsoft.EntityFrameworkCore.Design -v 1.1
项目 -> ManageNugetPackages -> 浏览 -> 搜索“Microsoft.EntityFrameworkCore.SqlServer”并安装或更新。
从 Nuget 安装以下软件包:-
Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Sqlite.Core
正如 Win 的最高得分答案所提到的,您可能需要安装 Microsoft.EntityFrameworkCore.SqlServer NuGet 包,但请注意,这个问题使用的是 asp.net core mvc。在最新的 ASP.NET Core 2.1 中,MS 包含了一个名为 Microsoft.AspNetCore.App 的元包
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2
如果您在解决方案资源管理器中右键单击 ASP.NET Core MVC 项目并选择 Edit Project File
,则可以看到对它的引用
如果 ASP.NET 核心 webapps 使用 using 语句,您应该会看到此元包
<PackageReference Include="Microsoft.AspNetCore.App" />
Microsoft.EntityFrameworkCore.SqlServer 包含在此元包中。因此,在您的 Startup.cs 中,您可能只需要添加:
using Microsoft.EntityFrameworkCore;
在 Visual Studio 中,检查 NuGet Package Manager => Manage Packages for Solution,检查所有这些包,无论是否安装在您的解决方案中,如下所示:
EntityFrameworkCore Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.InMemory Microsoft.EntityFrameworkCore.Relational Microsoft.EntityFrameworkCore.Sqlite.Core Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools
检查所有上述软件包已安装后,我解决了同样的问题。
我也有同样的问题。我添加了以下内容。这个对我有用
Microsoft.EntityFrameworkCore.SqlServer
在我的情况下:-我已经点击了下面的命令并且它已经解决了。命令
安装包 Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1
我将 SqlServer
和 Sqlite
添加到我的项目中,出现了同样的问题。
对我来说,我之前安装了 Microsoft.EntityFrameworkCore
,它的版本是 5.0.6
。现在 Microsoft.EntityFrameworkCore.SqlServer
版本是 5.0.7
。安装后没有UserSqlServer
方法。
1.尽量升级工具版本保持一致
尝试修改csproj中的版本:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7">
或打开 NuGet
以更新软件包。
2. 重启你的 Visual Studio
如果它对您没有帮助,最重要的是重新启动VS
然后,一切正常。
参考 https://github.com/dotnet/efcore/issues/7891
对我来说,这个问题发生在 Visual Studio Code 上,我可以通过 2 个步骤来解决:
使用 Microsoft.EntityFrameworkCore 手动添加;在终端中运行 dotnet build。
安装包,EntityFrameworkCore.SqlServer:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.3
努吉特:https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/
首先添加 Install-Package Microsoft.EntityFrameworkCore.SqlServer
接下来添加您的 .cs 文件 using Microsoft.EntityFrameworkCore;
最后将其添加到您的核心 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
}
哇这么多答案,但没有人提到这个 Microsoft.EntityFrameworkCore.InMemory 包!
添加对此包的引用:<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" />
,您应该一切顺利。
如果您在使用 Sqlite 时遇到此问题,那么
.我认为这是 Sqlite 版本的问题,我在使用这个版本的 SqLite 时遇到了同样的问题
2.2.4 版:
https://i.stack.imgur.com/9PaKs.png
https://i.stack.imgur.com/aNxEV.png
https://i.stack.imgur.com/U96DD.png
使用后没有错误
2.1.2 版:
https://i.stack.imgur.com/NdxWA.png
解决此问题的简单方法
https://i.stack.imgur.com/WrqzC.png
https://i.stack.imgur.com/7QbdF.png
https://i.stack.imgur.com/06nY9.png
https://i.stack.imgur.com/q4eqC.png
我阅读并做了每个答案中指示的所有内容,我刚刚发现我的问题是我的 DbContext 上缺少构造函数
public Context(DbContextOptions<Context> options) : base(options) { }
我希望这可以帮助任何面临同样问题的人。
我通过简单地解决了这个问题:
将 SqlServerDbContextOptionsExtensions
添加到有问题的班级 解决 SqlServerDbContextOptionsExtensions
这解决了问题,默认情况下必须缺少一些参考。
对于仍然遇到此问题的任何人:使用 NuGet 安装:Microsoft.EntityFrameworkCore.Proxies
此问题与将 Castle Proxy 与 EFCore 一起使用有关。
当我搬到 Microsoft.EntityFrameworkCore.SqlServer v3.0.0 和 Microsoft.EntityFrameworkCore.Tools v3.0.0 时遇到了这个问题
当我在两个库上改回 v2.2.6 时,错误就消失了。这更像是一种解决方法而不是解决方案,但它会让您启动并运行,直到问题得到解决。
对于 asp.net core 2.1 版,请确保添加以下包来解决问题。 (至少这解决了使用 SQLite 的问题)
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
这是使用带有实体框架核心的 SQLite 的文档的参考。 https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite
我遇到了这个问题,似乎我没有添加所需的 NuGet 包,虽然我认为我已经这样做了,但请务必一一检查。
目前正在使用 Entity Framework Core 3.1.3。以上解决方案都没有解决我的问题。
但是,在我的项目上安装包 Microsoft.EntityFrameworkCore.Proxies 解决了这个问题。现在我可以在设置 DBContext 选项时访问 UseLazyLoadingProxies() 方法调用。
希望这可以帮助某人。请参阅以下文章:
public AspDbContext(DbContextOptions<AspDbContext> options) : base(options) { }