openiddict-core icon indicating copy to clipboard operation
openiddict-core copied to clipboard

Cannot create a DbSet for 'OpenIddictToken' because this type is not included in the model for the context.

Open primalfear opened this issue 9 years ago • 7 comments

Hi There, I have recently updated to OpenIddict 1.0.0-beta2-0516 and when attempting to login using the password grant type, i receive the following error:

Cannot create a DbSet for 'OpenIddictToken' because this type is not included in the model for the context.

The database seed methods and everything work correctly. But when performing the autentication, it dies on this line: return this.SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);

StackTrace: System.InvalidOperationException: Cannot create a DbSet for 'OpenIddictToken' because this type is not included in the model for the context. at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]() at OpenIddict.EntityFrameworkCore.OpenIddictTokenStore4.get_Tokens() at OpenIddict.EntityFrameworkCore.OpenIddictTokenStore4.<CreateAsync>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at OpenIddict.OpenIddictProvider4.<SerializeRefreshToken>d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerHandler.<SerializeRefreshTokenAsync>d__25.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerHandler.<HandleSignInAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationHandler1.d__66.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationHandler1.<SignInAsync>d__66.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Http.Authentication.Internal.DefaultAuthenticationManager.<SignInAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.SignInResult.<ExecuteResultAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultAsync>d__30.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResultFilterAsync>d__28.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at URoute.Web.Infrustructure.Middleware.AngularServerMiddleware.<Invoke>d__3.MoveNext() in D:\Development\PetProjects\URoute\src\URoute.Web\Infrustructure\Middleware\AngularServerMiddleware.cs:line 32 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware1.d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware1.d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.SwaggerUi.Application.SwaggerUiMiddleware.<Invoke>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.SwaggerUi.Application.RedirectMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Swashbuckle.Swagger.Application.SwaggerMiddleware.<Invoke>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.Web.BrowserLink.Runtime.BrowserLinkMiddleware.<ExecuteWithFilter>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

DB Context: `public class URouteContext : OpenIddictDbContext { public static string DefaultSchema = "ur";

    [SuppressMessage("ReSharper", "SuggestBaseTypeForParameter")]
    public URouteContext(DbContextOptions<URouteContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
      OnModelCreating(builder);

        builder.AddConfiguration(new RouteMap());
        builder.AddConfiguration(new RouteRuleMap());
        builder.AddConfiguration(new UssdRequestMap());
    }

    public DbSet<Route> Routes { get; set; }
    public DbSet<RouteRule> RouteRules { get; set; }
    public DbSet<UssdRequest> UssdRequests { get; set; }
}`

OpenIddict Configuration `public static void AddAuthenticationServices(this IServiceCollection services) {

        services.AddIdentity<ApplicationUser, IdentityRole>(o => {
            o.Password.RequireDigit = false;
            o.Password.RequireLowercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequireUppercase = false;
            o.Password.RequiredLength = 3;
        })
        .AddEntityFrameworkStores<URouteContext>()
        .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        services.AddOpenIddict()
            .Configure(o => {
                o.AccessTokenLifetime = TimeSpan.FromHours(6);
                o.RefreshTokenLifetime = TimeSpan.FromDays(30);
            })
            .AddEntityFrameworkCoreStores<URouteContext>()
            .AddMvcBinders()
            .DisableHttpsRequirement()
            .EnableTokenEndpoint("/connect/token")
            .AllowPasswordFlow()
            .AllowRefreshTokenFlow()
            .AddEphemeralSigningKey();
    }`

primalfear avatar Dec 17 '16 12:12 primalfear

Did you read the "Update your Entity Framework context registration to register the OpenIddict entities" section in the README?

kevinchalet avatar Dec 17 '16 12:12 kevinchalet

If I add o.UseOpenIddict() as follows: services.AddEntityFrameworkSqlServer() .AddDbContext<URouteContext>(o => { o.UseSqlServer(connectionString); o.EnableSensitiveDataLogging(); o.UseOpenIddict(); });

Then I get: Cannot use table 'OpenIddictApplications' in schema '' for entity 'OpenIddictApplication' since it is being used for another entity. when the seed method runs.

primalfear avatar Dec 17 '16 13:12 primalfear

Please consider sharing a repro.

kevinchalet avatar Dec 17 '16 13:12 kevinchalet

I assumed i would not need to use o.UseOpenIddict() because my datacontext inherits from OpenIddictDbContext.

If i inherit from IdentityDbContext and use o.UseOpenIddict() Then when i run the seed method it throws the following:

Cannot create a DbSet for 'OpenIddictApplication' because this type is not included in the model for the context.

primalfear avatar Dec 17 '16 13:12 primalfear

OpenIddictDbContext is no longer a thing in beta2. Make sure you're not using old bits.

kevinchalet avatar Dec 17 '16 13:12 kevinchalet

Cool, thanks, will investigate this a bit further.... the repo, is currently hosted on gitlab, and i need to fiddle a bit to make it public.... but will pop a copy onto github, if i can't resolve this.

primalfear avatar Dec 17 '16 13:12 primalfear

Okay have resolved it.... One of my projects was still referencing: OpenIddict.EntityFramework package...

Thanks so much for the fast responses, was really not anticipated for a weekend. Really enjoying this as an identity solution.... am experimenting with it a bit having used identity server a lot in the past. :)

primalfear avatar Dec 17 '16 13:12 primalfear