Search code examples
.netentity-frameworkazure-functionsnpgsql

Entity framework works in console program but not in azure function


I am trying to build a solution architecture where I reference a .net project which contains all the code I need for an entity framework ORM which interfaces to a postgres database using the Npgsql provider.

Referencing such a project and making a query works in a console application but not in an azure function project.

Here is a link to a solution containing everything you need to reproduce this error. in ConsoleApp1, a reference to eftest is used and will run to completion without error.

The project named FunctionApp3 is the fail case. A Brand new azure function project targeting .net framework 4.71, referencing the eftest project and querying it. If you try the project, you will get this error.

Of course, when you actually install the Npgsql 4.0.2 package it so desperately craves, it changes to this error.

I thought this was a problem with a binding redirect. so I added an app.config similar to the one existing in the console app. Turns out that azure functions cant use app.config files, so I cant set a binding redirect or a providerName attribute, in the connection string. To the best of my knowledge, keyword port not supported is caused by not setting the providerName in the connection string.

Here is a log of the full rebuild of the azure function project.

Question: How do I get this entity framework project working with an azure function project?


Solution

  • @grek40 provided the solution for the second bug by placing ProviderName as a property in the settings.json.

    updated settings.json

    {
      "IsEncrypted": false,
        "Values": {
            "AzureWebJobsStorage": "",
            "AzureWebJobsDashboard": ""
    
        },
        "ConnectionStrings": {
            "DefaultConnection": {
                "ConnectionString": "Server=localhost;Port=5432;Database=ef-azf-ca-testdatabase;User Id=postgres;Password=postgres;Pooling=true;Max Auto Prepare=10;Auto Prepare Min Usages=1;"
                "ProviderName":  "Npgsql"
            }
        }
    }
    

    As for the first problem, I have no ide awhy I cant use Npgsql version 4.0.3 but it doesnt matter since I can just use 4.0.2. Help us Roji, you are our only hope.