简体   繁体   English

您的目标项目“XXX”没有引用 EntityFramework。 这个包是必需的

[英]Your target project 'XXX' doesn't reference EntityFramework. This package is required

Can anyone point me in the direction of where I have gone wrong.谁能指出我出错的方向。 I am trying to get migrations to work on EFCore with SQLite (Console App .NET Core 3.1)我正在尝试使用 SQLite(控制台应用程序 .NET Core 3.1)在 EFCore 上进行迁移

Running any command such as enable-migrations or update-database gives the error;运行任何命令,例如 enable-migrations 或 update-database 都会出现错误;

Your target project 'XXX' doesn't reference EntityFramework.您的目标项目“XXX”没有引用 EntityFramework。 This package is for the Entity Framework Core Tools to work.这个包是为了让 Entity Framework Core Tools 工作。 Ensure your target project is correct, install the package, and try again.确保您的目标项目正确,安装包,然后重试。

Seems a bit weird to need the EntityFramework if I am using EFCore??如果我使用 EFCore,需要 EntityFramework 似乎有点奇怪?

 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

In my case I was getting this same exceptions because I had the EntityFrameWork package installed along with EntityFrameWorkCore.就我而言,我遇到了同样的异常,因为我安装了 EntityFrameWork 包和 EntityFrameWorkCore。 I was using the later for some older functions I had in my Library which executed SqlCommand.我将后者用于执行 SqlCommand 的库中的一些较旧的函数。

Installing both packages completely scrubbed my project even after I removed the EntityFramework package.即使在我删除了 EntityFramework 包之后,安装这两个包也完全清理了我的项目。 Michael Browns comment gave me a hint as I ended up having to completely reset my EntityFramework Migrations by following this suggestion: Reset Entity Framework Michael Browns 的评论给了我一个提示,因为我最终不得不按照以下建议完全重置我的实体框架迁移: 重置实体框架

There's another way to do this:还有另一种方法可以做到这一点:

We need to use a factory for the DbContext to create migration in design time.我们需要使用 DbContext 的工厂来在设计时创建迁移。

We implement IDesignTimeDbContextFactory interface, because, for convention, when a class that implements this interface is found in the DbContext same project or in startup project, EFCore discard other ways to create the DbContext and will use the factory.我们实现 IDesignTimeDbContextFactory 接口,因为按照惯例,当在 DbContext 同一个项目或启动项目中发现实现该接口的类时,EFCore 会放弃其他创建 DbContext 的方式,将使用工厂。

See the implementation example:见实现示例:

  public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
  {

    public MyDbContext CreateDbContext(string[] args)
    {
      var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

      optionsBuilder.UseSqlServer("Your ConnectionString Here");

      return new MyDbContext(optionsBuilder.Options);
    }

After implement this, set the project that contains the DbContext as startup and default project and try to create the migrations:实现后,将包含 DbContext 的项目设置为启动和默认项目,并尝试创建迁移:

//.Net Core CLI:
dotnet ef migrations Add Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj

//Package Manager Console
Add-Migration Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj

Well, this was the strange solution and to my surprise it worked!嗯,这是一个奇怪的解决方案,令我惊讶的是它奏效了!

Just update the Visual Studio from VS installer and relaunch it.只需从 VS 安装程序更新 Visual Studio 并重新启动它。 This will remove all your unnecessary packages and re-install packages will solve your EF and EF core package conflicts.这将删除所有不必要的包,重新安装包将解决 EF 和 EF 核心包冲突。 Then run your migration and you will see the error will go away.然后运行您的迁移,您将看到错误将消失。

Hope this helps.希望这可以帮助。

I encountered this issue in project that I had both EF Core and EF6 installed.我在安装了 EF Core 和 EF6 的项目中遇到了这个问题。

I was constantly getting message while creating migrations:我在创建迁移时不断收到消息:

Both Entity Framework 6 and Entity Framework Core are installed. Entity Framework 6 和 Entity Framework Core 都已安装。 The Entity Framework 6 tools are running. Entity Framework 6 工具正在运行。 Use 'EntityFrameworkCore\\Add-Migration' for Entity Framework Core.对 Entity Framework Core 使用“EntityFrameworkCore\\Add-Migration”。

This was part of big solution (both .Net Framework and .Net Core ) and there is no way to make it use only one of them.这是大型解决方案( .Net Framework.Net Core )的一部分,没有办法让它只使用其中一个。

This was happening specifically on the Core part.这特别发生在核心部分。

What I tried and possibly what I would suggest is:我尝试过的,可能我会建议的是:

  1. Try repair your visual studio installation.尝试修复您的 Visual Studio 安装。

  2. Setup properly " Startup project " in your Visual Studio and " Default Project " in your Package manager console .Visual Studio中正确设置“启动项目”,在包管理器控制台中正确设置“默认项目”。

  3. Use For EF6:用于 EF6:

     EntityFramework\\Add-Migration <MIGRATIONNAME>

    For EF Core:对于 EF 核心:

     EntityFrameworkCore\\Add-Migration <MIGRATIONNAME>

Combination of these 3 things helped me out.这三件事的结合帮助了我。

As others haven't figured out, but I am sure having both EF6 and EF Core is culprit .正如其他人没有弄清楚的那样,但我确信同时拥有 EF6 和 EF Core 是罪魁祸首 I documented my journey here: https://github.com/dotnet/efcore/issues/27051我在这里记录了我的旅程: https ://github.com/dotnet/efcore/issues/27051

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite在Android的Unity项目上不起​​作用 - SQLite doesn't work on Unity project for Android 如何随您的应用程序分发所需的 *.dll 文件? - How to distribute required *.dll files with your application? Rails控制台:项目(表不存在?)-SQLite3 - Rails Console: Project(Table doesn't exist?) - Sqlite3 如何将xxx.db文件添加到我在android中的项目 - how to add xxx.db file into my project in android 在vscode for c++中使用sqlite3(undefined reference to `xxx')(我有使用-lsqlite3) - Use sqlite3 in vscode for c++(undefined reference to `xxx')(I have use -lsqlite3) 默认的Django身份验证架构不会在用户和组之间创建引用 - Default django auth schema doesn't create reference between users and groups Android Studio-项目复制后,SQLite数据库(getWritableDatabase函数)不起作用 - Android Studio - SQLite Database (getWritableDatabase function) doesn't work after a copy of the project 甚至都不进入方法。 错误“在空对象引用上调用虚拟方法java.lang.list” - Doesn't even get into the method. Error “invoke virtual method java.lang.list on a null object reference” ASP.NET 5:kpm无法找到EntityFramework.Sqlite和Microsoft.Data.Sqlite包 - ASP.NET 5: kpm can't locate EntityFramework.Sqlite and Microsoft.Data.Sqlite packages sqlite中的数据无法显示,错误“没有这样的列:xxx” - data in sqlite can't be shown, ERROR “No Such Column : xxx”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM