-
Notifications
You must be signed in to change notification settings - Fork 866
Make AddEntityFrameworkStores smarter with generics #1001
Comments
@lixiaoyuan what package versions are you using? |
project.json{ |
Note for triage: The call to services.AddIdentity<SystemUser<Guid>, BusinessDb.Cor.EntityModel.IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext,Guid>()
.AddDefaultTokenProviders(); Will try to register type UserStore<SystemUser, BusinessDb.Cor.EntityModel.IdentityRole, ApplicationDbContext, TKey> as the implementation for service type Note that this generic version of But our customer is trying to use a different generic version of public class SystemUser<TKey> : IdentityUser<TKey, IdentityUserClaim, IdentityUserRole, IdentityUserLogin>
where TKey : IEquatable<TKey>
{
}
...
public class IdentityRole : IdentityRole<Guid,IdentityUserRole,IdentityRoleClaim>
{
} Using the simpler versions works correctly: public class SystemUser<TKey> : IdentityUser<TKey>
where TKey : IEquatable<TKey>
{
}
public class IdentityRole : IdentityRole<Guid> {
} At this point I cannot remember if this is by design. cc @HaoK |
@lixiaoyuan from your code snippets it is not clear whether you could use the simpler generic versions of public class SystemUser<TKey> : IdentityUser<TKey>
where TKey : IEquatable<TKey>
{
}
public class IdentityRole : IdentityRole<Guid> {
} Could you clarify if in your scenario you actually need to use the base types that allow for full customization for some reason? |
You said I did not have to inherit the seven base class? But I still do not know the wrong code above the problem If you can not read what I'm saying, because I am using it translate.google.cn |
@lixiaoyuan yes I understand. You should be able to change the key type and add properties by inheriting from the base types that only have one generic argument. The explanation of why this currently doesn't work is in my previous comment at #1001 (comment). But please don't close the issue as I would like to discuss an approach for solving this with the team. |
@divega Yes Mr, and also ,Where on the Identity structure diagram? |
This was by design we don't have any sugar for the stores that use the more complicated generic parameters. |
I'm also running into this issue since I need to customise the UserRole entity, but I don't see a way of doing this while using the simpler base type.
Is there any way to work around this restriction while using an extended IdentityUserRole? |
1[System.Guid]', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
8
We think we should be able to make AddEntityFramework stores inspect the store to 'do the right thing' when using the base generic user store |
@divega I was able to get to the point where using reflection we can infer all of the child entity types using TUser. Unfortunately the last step appears to be not infer able, we need a concrete class for UserStore that knows how to materialize the child entitys, today we have: which is able to explicitly new up the default child entities. The problem when they plug in their own child entities, is that unless we ask them to specify the store type, we don't know how to implement these abstract types (we could switch directions and require the pocos to have a new() constraint instead)... So the best we can do without adding new constraints I think is to add something like: Thoughts? |
Note the |
Nevermind, we already have |
So the steps to make a custom generic base class EF store work is:
I don't think this is that unreasonable |
@HaoK From what I remember at the moment this should help. BTW, the worst part right is the exception you get if you deviate slightly from the only pattern that is supported. Can we improve that? |
Sure, I'll take a look what we can do to improve the error message, that's probably the only thing we can do, (Point them to use AddUser/RoleStore with their store instead of AddEntityFrameworkStores) |
How about these for the new exception messages:
|
Here is my code
ApplicationDbContext.cs
Identity.cs
Startup.cs
The text was updated successfully, but these errors were encountered: