gpt4 book ai didi

c# - 在 .net core 上创建 EF 迁移时出现 System.IO.FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 05:26:44 25 4
gpt4 key购买 nike

我有一个 .NET Core 2.2 项目,正在尝试设置 EF 迁移。

运行时:

dotnet ef migrations add init --verbose

我收到以下错误:

Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'... Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'. Finding design-time services referenced by assembly 'myproject-api'. No referenced design-time services were found. Finding IDesignTimeServices implementations in assembly 'myproject-api'... No design-time services were found. System.IO.FileNotFoundException: Could not load file or assembly 'myproject, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

这是我的 csproj 文件:

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

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4" />
</ItemGroup>

</Project>

编辑,这是我的 Program.cs

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseUrls("http://localhost:5004")
.UseStartup<Startup>();

}

最佳答案

EF Core 的设计时分辨率在很大程度上依赖于命名约定以及将配置放在正确的位置。不过,您的 Program 类看起来是正确的。

您能否检查一下您的 Startup 类是否与此类似,并且在 ConfigureServices 方法中具有所需的 AddDbContext 调用?

public class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
}

关于c# - 在 .net core 上创建 EF 迁移时出现 System.IO.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58459131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com