Azure / Azure/azure-functions-core-tools
DbContext registration throw exception
- Dominant language
- C#
- Stars
- 1.5k
- Forks
- 498
- Avg merge
- 5d 4h
- Merged PRs (30d)
- 15
Description
I get the exception `System.Private.CoreLib: Exception while executing function: FunctionName. MyCompany.AssemplyName: Method Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContextPool: type argument 'Project1DbContext' violates the constraint of type parameter 'TContext'.`
Environment:
Azure Functions Core Tools (2.4.401 Commit hash: 4e571e60f94a95283abf8a9fa363ec4d20b964a7)
Function Runtime Version: 2.0.12309.0
Structure:
I have one database connected from different projects and into the sources I have two layers to handle this:
1st - Handle all shared entities between projects (for example the users, etc..)
2nd - Different for each project, handle all project's entities (for example orders, etc..)
The entity `IdentityUser` is the base AspNetCore IdentityUser entity but it has some additional columns and, based on project, some different referenced tables.
All the identity tables relative to the user are inherited (IdentityRole, IdentityUserClaim, IdentityUserRole etc..) due to customization of the primary key type
So, the code looks like:
*BaseIdentityUser.cs* Used to overwrite base IdentityUser entity due to the presence of additional columns and change of the primary key type from `Guid` to `int`
```c#
public class BaseIdentityUser: AspNetCoreIdentity.IdentityUser
{
// Properties of additional columns
}
```
*BaseDbContext.cs* Base context derived from IdentityDbContext of AspNetCore used to define all Identity tables with different configuration and all the tables shared among company projects
```c#
public class BaseDbContext
: AspNetCoreIdentity.IdentityDbContext<
TIdentityUser,
CustomModels.IdentityRole,
int,
CustomModels.IdentityUserClaim,
CustomModels.IdentityUserRole,
CustomModels.IdentityUserLogin,
CustomModels.IdentityRoleClaim,
CustomModels.IdentityUserToken>
where TIdentityUser : CustomModels.BaseIdentityUser
{
public BaseDbContext(DbContextOptions options)
: base(options) { }
// Properties of shared tables
}
}
```
*Project1IdentityUser.cs* Used to define project1 user's relative tables
```c#
public class Project1IdentityUser: BaseIdentityUser
{
// Properties of relative tables
}
```
*Project1DbContext.cs* Used in project1 and defines the type of IdentityUser entity
```c#
public class Project1DbContext: BaseDbContext
{
// Properties of project1 tables
}
```
The context is registred in this way;
```c#
services.AddDbContextPool(dbContextOptions =>
{
dbContextOptions.UseSqlServer(
"ConnectionStringName",
sqlServerOptions =>
{
sqlServerOptions.UseNetTopologySuite();
sqlServerOptions.EnableRetryOnFailure(
maxRetryCount: 3,
maxRetryDelay: TimeSpan.FromSeconds(5),
errorNumbersToAdd: null);
});
});
```
I get this exception even if I try to register the BaseDbContext, only trying to register the base DbContext class works these lines of code.
I have 6 other AspNetCore 2.2 projects which use this structure and all of these work fine.
I think that the problem is related to the `BaseDbContext` which have the `TIdentityUser` as a type passed from outside, but why not works only if I use this code in Azure Function?
Contributor guide
Research direction
Start with the AddDbContextPool registration and the BaseDbContext constraint shown in the issue. Reproduce it with Azure Functions Core Tools 2.4.401 and runtime 2.0.12309.0, then compare the registration with the six working ASP.NET Core 2.2 projects. Done means documenting the cause and the registration path that works for the derived context.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100