【问题标题】:The specified deps.json does not exists in .NET 6.NET 6 中不存在指定的 deps.json
【发布时间】:2023-02-01 14:09:38
【问题描述】:

我在 .NET 6 上的 Blazor 中运行 add-migration 命令。解决方案构建成功,但出现以下错误 - 如何修复?

指定的 deps.json [C:\Users\User\Desktop\Application\BlazorWebApplication\BlazorWebApp\bin\Debug\net6.0\BlazorWebApp.deps.json] 不存在

【问题讨论】:

  • 您不能在浏览器上使用 EF。浏览器中没有数据库将迁移添加到客户项目。
  • 那么我们不能将实体框架与 Blazor wasm 一起使用吗
  • 浏览器中没有数据库。你需要 ORM 做什么? Blazor WASM 就像 React 和 Angular 一样是一个 SPA。这些也无法连接到任何数据库。这是他们使用的连接到数据库的后端服务。如果你创建一个托管的 WebAsm 项目,宿主项目中的服务可以连接到数据库并使用 ORM
  • 所以你的意思是我必须使用外部 api 项目进行数据访问
  • 这看起来像一个错误。迁移应该在 Blazor 项目中工作。还有,你能够在 wasm 中使用 EF Core。 SQLite 在 wasm 上的浏览​​器中工作得很好,我认为 Azure Cosmos DB 客户端也可以从 wasm 连接,因为它只发送 HTTP 请求。

标签: c# entity-framework-core blazor


【解决方案1】:

我向 Entity Framework 团队开了一张票,他们为这个问题提供了这个解决方法:

解决方法是在解决方案中创建一个单独的控制台应用程序项目。该项目仅用于使 EF 工具能够创建适当的上下文实例。该项目需要引用包含您的 DbContext 和您正在使用的 EF Core 数据库提供程序的项目。例如:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="6.0.12" />
        <PackageReference Include="SQLitePCLRaw.core" Version="2.1.3" />
        <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.3" />
        <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.1.3" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..ThePlayerSharedThePlayer.Shared.csproj" />
    </ItemGroup>

</Project>

控制台应用程序需要有一个IDesignTimeDbContextFactory。是这样的:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using ThePlayer.Shared.Data.Context;

Console.WriteLine("Hello, World!");

public class ContextFactory : IDesignTimeDbContextFactory<ThePlayerContext>
{
    public ThePlayerContext CreateDbContext(string[] args) 
        => new(null, new DbContextOptionsBuilder<ThePlayerContext>().UseSqlite(args[0]).Options);
}

然后可以使用它从命令行生成迁移。例如:

PS C:localcode
eprosThePlayer-masterThePlayer-masterConsoleApp1> dotnet ef migrations add One --project ..ThePlayerSharedThePlayer.Shared.csproj -- "DataSource=ThePlayer.db"
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
PS C:localcode
eprosThePlayer-masterThePlayer-masterConsoleApp1>

来源:How do generate migrations for an Blazor WASM

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 2020-05-27
    • 2019-11-11
    • 1970-01-01
    • 2019-10-23
    • 2023-02-01
    • 2021-08-31
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多