Azure-functions-core-tools: Registration of a type derived from DbContext throws "Violates the constraint of type parameter DbContext"

Created on 25 May 2019  路  20Comments  路  Source: Azure/azure-functions-core-tools

We have an asp.net core 2.2 web API which works perfectly. We decided to move to Azure Durable Functions and hence started using IFunctionsHostBuilder to wire up the very same services. When running the application, the following exception is thrown.

[2019-05-25 6:04:29 PM] A host error has occurred
[2019-05-25 6:04:29 PM] MyDurableFunctionApp.Function: GenericArguments[0], 'MyDurableFunctionApp.Database.Context.AccountManagementDbContext', on 'MyDurableFunctionApp.Repository.DbContextTransaction`1[TDbContext]' violates the constraint of type parameter 'TDbContext'.

AccountManagementDbContext is derived indirectly from DbContext class defined in EF and TDbContext is a generic type which expects DbContext like the following code snippet:

public class DbContextTransaction<TDbContext> :  IDisposable where TDbContext : DbContext   
{
}

This is how the Azure Function's defined:

<PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>

There are two nuget packages that may be important to consider which are:

  • Microsoft.EntityFrameworkCore.Tools ver 2.2.4
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore ver 2.2.0

This issue is a 100% blocker and unfortunately, we had to drop pursuing the path of Azure Functions until hopefully the issue is resolved by Microsoft.

FYI: @miladghafoori

Needs

All 20 comments

Hard to say - can you provide a repro or at least give an example of how you are registering the services in the FunctionsStartup

@jeffhollan I will prepare and share a small repo.
FYI: @miladghafoori

@jeffhollan
There is a ReadMe file in the following repository that explains everything. Hope this repo helps you guys find out and address the root cause of the issue:

https://github.com/Arash-Sabet/DbContextRegistrationIssueAzureFunctionsv2/tree/master

FYI: @miladghafoori

Repro'd, but once I dropped the version of EntityFramework and EntityFramework.SqlServer to 2.2.3 it was happy and works. I expect this is related to this:

https://github.com/Azure/azure-functions-host/issues/4006

I'll flag this as this appears to be a common pitfall. I don't fully understand all the reasons here but that should get you unblocked.

/cc @fabiocav

Thanks @jeffhollan for trying it out. I'm not sure if we can downgrade the mentioned nuget packages in our mainstream project as it may or might cause some other issues. How likely you guys can expedite addressing this issue as it's quite a significant blocker of our progress? That'll be appreciated.

/cc @miladghafoori & @fabiocav

I would be quite surprised if the difference between 2.2.3 and 2.2.4 would interfere with mainstream project. We'll look into this soon - flagged it as high on bug backlog.

Closing this now - will track the main pieces in azure-functions-host#4006

I would be quite surprised if the difference between 2.2.3 and 2.2.4 would interfere with mainstream project. We'll look into this soon - flagged it as high on bug backlog.

Well, the reason for my concern is that there are a good number of other nuget packages (internal and Microsoft's) that depend on each other. So, any downgrades in the two mentioned package may possibly cause downgrading in other packages and resulting in unforeseen situations. I will discuss it internally while waiting for the bug to be fixed which is preferred. /cc @miladghafoori

@jeffhollan Just wondering, how would I know whether or not this issue is resolved or scheduled to release? Thanks. /cc @miladghafoori

This issue is tracking and when it closes would be easiest signal that the versions have been updated to support latest Azure/azure-functions-host#4006

@jeffhollan Just FYI: I have downgraded the mentioned packages and the issue still persists in our mainstream project.

I just wrote a blog and published a sample project using EF with latest release. Can you check this out and see if any version that鈥檚 different from what you鈥檙e trying?

http://dev.to/azure/using-entity-framework-with-azure-functions-50aa

Thanks @jeffhollan , I read the article when it was published the other day. Wiring up our services and classes in our asp.net core project works perfectly but not in a function app. The very same extension methods that we composed to wire up the services are invoked as is in the function app. Not sure what else to be done in our working code to satisfy functions. I suspect that there is a difference between the way that dependency injection framework works for functions vs. asp dotnet core.

/cc @miladghafoori

Yeah I can confirm that when you drop the version of EF to 2.2.3 the error in the above issue (violating the constraint) goes away, but when trying to call the HTTP Function I do see an error about authentication. In this case you are trying to override / use custom auth middleware which is slightly different as dependency injection works the same between functions and asp.net, but you can't register or assume all the components are 1:1. In this case I expect there are some gaps in custom auth middleware configurations via DI as you are attempting to do in the above sample. It's not an invalid ask, but likely a separate issue. It may even be this issue:

https://github.com/Azure/azure-functions-host/issues/4397

@jeffhollan apparently our issue is similar to Azure/azure-functions-host#4397

/cc @miladghafoori

I initially raised an issue over this in August last year, and given the recent update to allow proper DI in Azure Functions I thought I would give it a whirl and I was disappointed to see that the exact same issue persists 10 months later! I have updated my demo repro the demonstrates an MVC project working fine, and an Azure Functions project breaking when trying to use the same IdentityDbContext.

Does anyone know if this will be resolved in ASP.NET Core 3?

@benm-eras I left a note in Azure/azure-functions-host#4397 yesterday hoping for communicating the urgency of fixing this issue. Unfortunately, this issue has become a significant roadblock for us too. It will be great to hear from Microsoft's relevant team's folks on an ETA when this fix becomes available.

@jeffhollan @miladghafoori

Just a follow up question as there was a recent comment after the issue was closed. Are you still experiencing issues with the same version of EF? Azure Functions does lag behind .NET Core/ASP.NET Core releases a bit, as we depend on those updates being properly deployed to App Service, so this issue will often come up if you're adopting those changes as soon as they're released and jumping ahead.

@miladghafoori do you happen to have some time these days to update all nuget packages on the functions branch of the application to verify whether or not we're still experiencing the same issue? I think it's a great idea to install the latest .net core 2.2 SDK too on your machine and give it a spin. Otherwise, we have to wait for a couple of weeks for me to get back to the question raised by @fabiocav to confirm whether or not the EF issue still persists.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings