Skip to content

Problem with AppDbContext derived from IdentityDbContext #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
milosloub opened this issue Apr 4, 2018 · 4 comments · Fixed by #250
Closed

Problem with AppDbContext derived from IdentityDbContext #249

milosloub opened this issue Apr 4, 2018 · 4 comments · Fixed by #250
Labels

Comments

@milosloub
Copy link
Contributor

Hello,
after update from 2.1.11 to 2.2, there si error in Startup.cs inicialization:
services.AddJsonApi<AppDbContext>

Type does not implement 'IIdentifiable'\r\nParameter name: resourceType

Stack trace:

at JsonApiDotNetCore.Builders.ContextGraphBuilder.GetIdType(Type resourceType)
at JsonApiDotNetCore.Builders.ContextGraphBuilder.AddDbContextT
at JsonApiDotNetCore.Extensions.IServiceCollectionExtensions.<>c__21.<AddJsonApi>b__2_0(IContextGraphBuilder builder) at JsonApiDotNetCore.Configuration.JsonApiOptions.BuildContextGraph(Action1 builder)
at JsonApiDotNetCore.Extensions.IServiceCollectionExtensions.AddJsonApi[TContext](IServiceCollection services, Action`1 options, IMvcBuilder mvcBuilder)

If AppDbContext derived from DbContext, everything is ok, but my AppDbContext derived from IdentityDbContext<User,Role,int>.

Can you make some investigation? Thank you.

@jaredcnance jaredcnance added the bug label Apr 4, 2018
@jaredcnance
Copy link
Contributor

jaredcnance commented Apr 4, 2018

So, this is the problem area:

private Type GetIdType(Type resourceType)
{
var interfaces = resourceType.GetInterfaces();
foreach (var type in interfaces)
{
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(IIdentifiable<>))
return type.GetGenericArguments()[0];
}
throw new ArgumentException("Type does not implement 'IIdentifiable<TId>'", nameof(resourceType));
}

I'm not sure right now why this was ever working. The correct behavior would be to log a warning rather than throw the exception. The reason it fails with the IdentityDbContext is because the Identity models do not implement IIdentifiable which should be handled. And we shouldn't require all models on a context to be JsonAPI resources. Depending on the size of your context you could temporarily manually construct the context graph:

services.AddJsonApi(options => {
        options.BuildContextGraph((builder) => {
            builder.AddResource<MyModel>("my-models");
        });
    }, mvcBuilder);

https://json-api-dotnet.github.io/#/context-graph

I will work on having a fix in v2.2.1 as soon as possible.

@milosloub
Copy link
Contributor Author

For me this temporary solution is ok until it is fixed.
Btw thank you for this library. It's total masterpiece

jaredcnance added a commit that referenced this issue Apr 4, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
fix(ContextGraphBuilder): don't throw if DbContext contains non json:api resource
@jaredcnance
Copy link
Contributor

@milosloub please upgrade to 2.2.1 and let me know if that solves your problem.

@milosloub
Copy link
Contributor Author

@jaredcnance Wow, that was fast. After upgrade it works perfectly. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants