EF Core 查询出现错误“无法加载文件或程序集 'Npgsql。系统找不到指定的文件。”

问题描述 投票:0回答:1

我正在使用 .NET 6 和 PostgreSQL 数据库。

我有一个

hosted service
,它定期调用
repository's function
并且
querying database entities
使用
EF Core
,如下所示:

public class NotificationRepository : INotificationRepository
    {
        public async Task<List<Notification>> GetUnreadDeviceNotifications()
        {
            using var context = new MyDbContext();
            return await context.Notifications.Where(x => !x.IsSentTimestamp.HasValue && x.ChannelId == 8).ToListAsync();
        }
    }

一旦我在

linux
机器上部署并且每次服务调用
GetUnreadDeviceNotifications
时,我都会收到此错误:

Aug 07 12:50:46 my-app my-app.Web[1432629]: System.IO.FileNotFoundException: Could not load file or assembly 'Npgsql, Version=6.0.8.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. The system cannot find the file specified.
Aug 07 12:50:46 my-app my-app.Web[1432629]: File name: 'Npgsql, Version=6.0.8.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension.ExtensionInfo.GetServiceProviderHashCode()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContextOptions.GetHashCode()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContext.get_Model()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at my-app.Infrastructure.Repositories.NotificationRepository.GetUnreadDeviceNotifications() in C:\Projects\my-app\src\my-app.Infrastructure\Repositories\NotificationRepository.cs:line 22
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at my-app.Application.Services.MessagingServices.NotificationReadingService.DoWork(Object state) in C:\Projects\my-app\src\my-app.Application\Services\MessagingServices\NotificationReadingService.cs:line 35

这些是

dependencies
我有:

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
    <PackageReference Include="Npgsql" Version="6.0.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
  </ItemGroup>

附注看起来效果很好

Windows

c# .net postgresql entity-framework entity-framework-core
1个回答
0
投票

实际上我有一个具有以下配置的项目:

Project Config

Program.cs or StartUp.cs

My Hosted Service

我可以连接到数据库并获取查询结果。

请记住在 Startup.cs 或 Program.cs 中声明您的存储库服务。 另一件需要注意的重要事情是,您无法从托管服务访问存储库实例,因为它是单例,因此访问存储库的最佳方法是使用 IServiceProvider 实例。 我的英语说得一点都不好。

© www.soinside.com 2019 - 2024. All rights reserved.