简体   繁体   中英

.Net Core 3 preview 5 with Entity Framework for Postgresql

I am trying to migrate my core 2.2 project (works fine at 2.2) to 3.0 preview. And I have problem migrating my repositories (Entity Framework).

I've got following error message:

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Query.Pipeline.IEntityQueryableTranslatorFactory' while attempting to activate 'Microsoft.EntityFrameworkCore.Query.Pipeline.QueryCompilationContextFactory2'.

I've got following list of dependencies:

  • AutoMapper 8.1.1
  • AutoMapper.Extensions.Microsoft.DependencyInjection 6.1.1
  • Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.0.0-preview6.19307.2
  • Microsoft.EntityFrameworkCore 3.0.0-preview6.19304.10
  • Npgsql.EntityFrameworkCore.PostgreSQL 3.0.0-preview5

Following code fails on:

_context.Some.Where(x => x.UserId == userId).ToListAsync();

=>

public class MyRepository
    {
        private readonly MyContext _context;

        public MyRepository(MyContext context)
        {
            _context = context;
        }

        public Task<List<MyEntity>> GetByUserIdAsync(string userId)
        {
            return _context.Some.Where(x => x.UserId == userId).ToListAsync();
        }
    }

Startup contains:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddMvc().AddNewtonsoftJson();

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddScoped<MyRepository, MyRepository>();

        services.AddEntityFrameworkNpgsql()
            .AddDbContext<MyContext>()
            .BuildServiceProvider();

        services.Configure<PostgresDatabaseConfiguration>(
            _config.GetSection(PostgresDatabaseConfiguration.ConfigName));

        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
        app.UseRouting();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });           
    }        

It seams to my, that I am plug in EF incorrectly, so DI do not work. Any ideas how to fix this?

After upgrading .net core 3 preview 6 i had similar issue..after lots of research of i got this issue regarding t version incompatibility in postgresql.

https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/903

it will be fix n next release ..wait for some days to release preview 7

我设法通过降级到Microsoft.EntityFrameworkCore 3.0.0-preview5来解决此问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM