Azure / azure-functions-core-tools

Command line tools for Azure Functions
MIT License
1.32k stars 433 forks source link

DbContext registration throw exception #1084

Open Dona278 opened 5 years ago

Dona278 commented 5 years ago

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

public class BaseIdentityUser: AspNetCoreIdentity.IdentityUser<int>
{
      // 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

public class BaseDbContext<TIdentityUser> 
      : 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

public class Project1IdentityUser: BaseIdentityUser
{
       // Properties of relative tables
}

Project1DbContext.cs Used in project1 and defines the type of IdentityUser entity

public class Project1DbContext: BaseDbContext<Project1IdentityUser>
{
       // Properties of project1 tables
}

The context is registred in this way;

services.AddDbContextPool<Project1DbContext>(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?

miladghafoori commented 5 years ago

@Dona278: I am having a similar issue. Were you able to resolve this?

Arash-Sabet commented 5 years ago

@Dona278 @miladghafoori This is a blocker and it has to be resolved by Microsoft.