Azure-functions-host: Use of IdentityDbContext not supported in Azure Function?

Created on 23 Jul 2018  路  22Comments  路  Source: Azure/azure-functions-host

_From @gizmo3399 on July 7, 2018 20:53_

I am trying to create an Azure Function that modifies some data in an SQL database.
I have setup EntityFrameworkCore in a separate .NETStandard 2.0 class library.
In the web project I configured EF for I make use of the Identity service provided by the web template (individual user accounts) therefore I use the IdentityDbContext over the regular DbContext.

I have referenced the class library in my functions project which is a (V2 preview azure functions project).

var options = new DbContextOptionsBuilder<HumbleGameKeysContext>();
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:HumbleGameKeysContext");
options.UseSqlServer(connectionString);
return new HumbleGameKeysContext(options.Options);

If i instantiate my DbContext like above, I get the following error:
image
Even though this is the exact same way it is instantiated in the AspCore web project.

If I instantiate it without a connection string it creates a context but when getting all users on it I get the following error:
image

I am not sure if this is because it has no connection string, but I would expect some kind of connection error instead.

But when I change the HumbleGameKeysContext into a regular DbContext instead of a IdentityDbContext everything works fine and I can instantiate a context using the above code.
Retreiving data from the Database also works.
But for the function I want to write I actually need to access the Users DbSet which only exists on the IdentityDbContext.
I could separate the data I need to access into a separate table or I could maybe model the Users table myself. But I am interested to know if this can be supported out of the box.

But for now it seems that an IdentityDbContext is not supported or did I make a mistake anywhere?

I have used these tools and NuGet packages:

  • Visual Studio 2017 v15.7.4
  • Azure Functions and Web Job Tools extension v15.0.40617.0
  • Azure Functions CLI tools v2.2.2
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore v2.1.1
  • Microsoft.EntityFrameworkCore v2.1.1
  • Microsoft.EntityFrameworkCore.SqlServer v2.1.1
  • Microsoft.NET.Sdk.Functions v1.0.14

If anyone has got this working before or has any information that could help I would appreciate it.

_Copied from original issue: Azure/Azure-Functions#878_

assembly-load

Most helpful comment

This is being addressed with the release currently rolling out: https://github.com/Azure/app-service-announcements/issues/180

We're also going to post updates to the .NET Core/ASP.NET Core version supported by functions on the same announcements repo.

All 22 comments

Can you please attach a simple repro project? It will make it easier to investigate.

_From @gizmo3399 on July 10, 2018 22:7_

Azure functions IdentityDbContext Repro.zip
Hi @pragnagopa

I have attached a stripped down version of my project.
I have switched EF to use the in-memory database to make testing easier.

You can verify that the IdentityDbContext is working in the web project.
You can log in with the following credentials: username: [email protected] password: Test123

If you run the Azure functions project you will get the above mentioned error.
I have also included a TestContext which is based on the normal EF DbContext so you can verify that that one does not cause errors in the functions project.

I create the context in the SetupDatabase method of the ResetClaimingPeriod function

Please note that i was using .NET Core 2.1.1 SDK so you might need to install it.

If you need more info or having trouble running the repro project let me know.

_From @Doddler on July 19, 2018 21:44_

I created a bare-bones repository that cleanly represents the issue over at: https://github.com/Doddler/FunctionsIdentityTest

Function1 attempts to instantiate an empty context derived from IdentityContext. Function2 does the same for an context derived from DbContext. Function2 works, but Function1 always returns the following:

[2018-07-19 9:42:52 PM] Executed 'Function1' (Failed, Id=e486157c-740c-4de5-8be1-d3d16e6bfe28)
[2018-07-19 9:42:52 PM] System.Private.CoreLib: Exception while executing function: Function1. FunctionsIdentityTest: GenericArguments[0], 'FunctionsIdentityTest.TestDbIdentityContext', on 'Microsoft.EntityFrameworkCore.DbContextOptionsBuilder`1[TContext]' violates the constraint of type parameter 'TContext'.

I'm using .NET Core 2.1.2 SDK with azure-functions-core-tools 2.0.1-beta.31.

_From @Doddler on July 19, 2018 23:45_

After additional testing, I found that downgrading the EntityFrameworkCore and Identity packages from 2.1.1 to 2.1.0 resolved the issue. Some kind of regression somewhere perhaps...?

_From @gizmo3399 on July 20, 2018 13:50_

Thanks @Doddler!

I have also downgraded to version 2.1.0 of EF Core and Identity and can confirm it is now working
I also saw today that version 2.1.2 was available for Microsoft.AspNetCore.Identity.EntityFrameworkCore and tried with that. But sadly no luck.

I will use version 2.1.0 for now but hope this issue gets fixed so I can go back to the latest version later.

Thanks for your help!

_From @espray on July 20, 2018 16:3_

+1 downgraded to 2.1.0 as workaround

@Doddler - Thank you for the detailed repro.
@fabiocav - Can you please take a quick look?

_From @fabiocav on July 21, 2018 1:12_

This is because that is currently flagged as a runtime restricted assembly and the runtime currently runs on 2.1.0. This will be updated on the next release.

Any update on this, I need to use v 2.1.3 of Microsoft.AspNetCore.Identity.

This is rolling out with update 2.0.12134. The update should be completed on Azure by the end of the week, with tooling updates following that.

I get this error again:
Azure Functions Core Tools (2.1.748 Commit hash: 5db20665cf0c11bedaffc96d81c9baef7456acb3)
Function Runtime Version: 2.0.12134.0
Microsoft.AspNetCore.Identity.EntityFrameworkCore: 2.1.3
Microsoft.EntityFrameworkCore: 2.1.4

and I found downgrade ef core from 2.1.4 to 2.1.3 can resolve this issue.
Is there any way to break the strong bind between the azure function host and some other nuget packages version?

This is not resolved with update 2.0.12134.0. I tried it on both an existing and a new blank project. Downgrading from 2.1.4 to 2.1.3 didn't work for me.

The same thing here as @cbfrank has. The fix is very desirable, @pragnagopa

Ive still got the same problem as well. Created an issue here before i found this one:

https://github.com/Azure/azure-functions-core-tools/issues/847

@nikolayvpavlov your issue seems to be a bit different, would you mind providing a link to a repository with a repro for this to ensure we have your exact setup?

@Bazeloth following up on the linked issue

@cbfrank @stukalin we'll take a look and provide an update on this

@fabiocav , I created a demo project, please check it out here.

This is now erroring if you dare use .NET Core 2.2.1

When is functions going to be fixed so that it reliably works with new versions especially point releases?

@JohnGalt1717 can you please open an issue with details on the error you鈥檙e seeing?

@fabiocav see #4006

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 just tested using 2.2.0 and it is working, but it is broken in 2.2.4.

So it looks like at some point it was fixed, but the bug has most probably been reintroduced since 2.2.0

This is being addressed with the release currently rolling out: https://github.com/Azure/app-service-announcements/issues/180

We're also going to post updates to the .NET Core/ASP.NET Core version supported by functions on the same announcements repo.

Was this page helpful?
0 / 5 - 0 ratings