npgsql / efcore.pg

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

Npgsql.EntityFrameworkCore.PostgreSQL Error when used in Azure Functions in VS 2017 #687

Closed neeldeep closed 5 years ago

neeldeep commented 5 years ago

I am not sure if any one has come across this issue before. I am using EF Core and Npgsql.EntityFrameworkCore.PostgreSQL inside Azure functions to fetch data from a Postgres Database. When I am using the same code in a Console App it works fine, However when used inside Azure functions it throws the following error:

System.Private.CoreLib: Exception while executing function: Function1. 
Npgsql.EntityFrameworkCore.PostgreSQL: 
Method Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsBuilderInfrastructure.AddOrUpdateExtension: 
type argument 'Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension' violates the constraint of type parameter 'TExtension'

I am using the following DLLs: Microsoft.EntityFrameworkCore Npgsql.EntityFrameworkCore.PostgreSQL Npgsql.EntityFrameworkCore.PostgreSQL.Design

The same code works perfectly inside an Azure function when running from Visual Studio for MAC.

austindrenski commented 5 years ago

@neeldeep Could you post some version information for the those libraries? Or perhaps a complete .csproj file that reproduces this issue?

neeldeep commented 5 years ago

Here is the .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.*" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Please note when I run the same code inside a Console App it works fine but not when I use it in an Azure function.

austindrenski commented 5 years ago

Since this works locally but not in Azure, seems like it could be dependency/restore related.

You should be able to drop both of the following dependencies as the first is implicitly included by referencing the PostgreSQL provider and the second is fully deprecated:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />

Could you try updating with the above and report back on how it goes?

neeldeep commented 5 years ago

I am testing the Azure Functions locally and have not deployed to Azure. This is not working for me in the local environment. I was using the second to generate the Tables and DBContext class from dot net cli. I will remove them and try once.

neeldeep commented 5 years ago

This seems to be working now. Closing the issue. Is it because the EF core was added even though it is present implicitly as part of PostgreSQL?

austindrenski commented 5 years ago

I'm actually not sure. I suspect it was some type of refelction or dependency injection conflict from having the reference to the *.Design library included.

Note that you can still scaffold without that library, as recent versions incorporate that code directly. (This was briefly mentioned in the 2.0 release notes.)

Claytonious commented 5 years ago

Ran into the same issue with an Azure Functions project that includes <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />. It was also referencing Entity Framework Core directly and I had this same error from within Azure, but not from unit tests and a command line project. Removing the explicit reference to the Entity Framework Core project fixed the error here, too, just as it did for @neeldeep .

This is in spite of the fact that I was definitely including the exact version of Entity Framework Core that Npgsql.EntityFrameworkCore.PostgreSQL was dependent on.

The state of nuget dependencies in .net core is deeply mysterious and dreadful and brittle and complicated.

roji commented 5 years ago

@Claytonious directly referencing EF Core shouldn't be a problem, as long as you're referring a version that is compatible with the version of Npgsql.EntityFrameworkCore.PostgreSQL. For example, it should be OK to reference EF Core directly to get a newer patch version from what Npgsql.EntityFrameworkCore.PostgreSQL brings in - because EF Core sometimes releases more frequently than Npgsql. But if you try to mix minor or major versions things will obviously start failing.

Aside from that referencing obsolete packages (e.g. Design) from version 1.x is certain not to go well...