npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.54k stars 226 forks source link

Npgsql EF Core 3.1 migration dependency error #1447

Closed rbiq closed 4 years ago

rbiq commented 4 years ago

Steps to reproduce

dotnet ef migrations add InitialCreate --context MyContext --output-dir ./src/services/Data/Migrations --project ./src/services/Core --startup-project ./src/services/Api

The issue

I'm getting the below while trying to run EFCore 3.1.6 migration in asp.net core 3.1 & .NETStandard 2.1

Exception message:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMappingSourcePlugin Lifetime: Singleton ImplementationType: Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlNodaTimeTypeMappingSourcePlugin': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Storage.ISqlGenerationHelper' while attempting to activate 'Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlNodaTimeTypeMappingSourcePlugin'.) (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Query.IMethodCallTranslatorPlugin Lifetime: Singleton ImplementationType: Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.NpgsqlNodaTimeMethodCallTranslatorPlugin': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Query.ISqlExpressionFactory' while attempting to activate 'Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.NpgsqlNodaTimeMethodCallTranslatorPlugin'.) (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.Query.IMemberTranslatorPlugin Lifetime: Singleton ImplementationType: Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.NpgsqlNodaTimeMemberTranslatorPlugin': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Query.ISqlExpressionFactory' while attempting to activate 'Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.NpgsqlNodaTimeMemberTranslatorPlugin'.)

Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Stack trace:

Further technical details

asp.net core 3.1.302/3.1.6 csproj details

<ItemGroup>
    <PackageReference Include="EFCore.NamingConventions" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="3.1.6" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
    <PackageReference Include="EnumExtensions.System.Text.Json" Version="1.0.0" />
    <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.6">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="NodaTime" Version="3.0.0" />
    <PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="Npgsql" Version="4.1.3.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
    <PackageReference Include="Npgsql.NodaTime" Version="4.1.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="3.1.4" />
    <PackageReference Include="System.Text.Json" Version="4.7.2" />
    <!--<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.6" />-->
    <!--<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.6" />-->
  </ItemGroup>

.NetStandard 2.1 csproj details

<ItemGroup>
    <PackageReference Include="EFCore.NamingConventions" Version="1.1.0" />
    <PackageReference Include="Npgsql" Version="4.1.3.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
    <PackageReference Include="Npgsql.NodaTime" Version="4.1.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="3.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.6">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="System.Text.Json" Version="4.7.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
    <PackageReference Include="NodaTime" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" />
  </ItemGroup>
rbiq commented 4 years ago

it was my mistake. was missing in startup.cs services.AddEntityFrameworkNpgsql(); services.AddEntityFrameworkNpgsqlNodaTime();

roji commented 4 years ago

Note that you usually shouldn't be using thee AddEntityFramework* methods, use UseNpgsql instead (here's an ASP.NET tutorial, substitute Npgsql for SqlServer).