-
Notifications
You must be signed in to change notification settings - Fork 866
How to include Roles in IdentityUser? #1361
Comments
Once I add this to my poco object, another issue come up, it seems IdentityRole property changed from RoleId to Id |
hi @andrew-vandenbrink same issue here although i got a little further: it has to be then i added the following in OnModelCreating
then i got the following during migration:
so i dunno why string is not compatible with string as this was used in efcore 1.1 to join them... |
@JanEggers thanks, as you suggest it should be IdentityUserRole instead of IdentityRole I still have another issue during upgrading the razor thus I can't test your case yet... |
I just "stole" a line from the sources
|
how can i migrate to core 2.0? |
@adeministr I did the following which was enough
|
@MaximBalaganskiy tried your code, but it does not work for me. when i add a migration ef still creates another MyUserId column like that: (from ModelSnapshot)
that is quite confusing as the modelbuilder in onmodelcreating seems to pick it up correctly (OnModelCreating => builder.Model.DebugView)
|
I did have a duplicate column before I added the manual mapping for the
navigation property
…On 17 Aug 2017 3:52 PM, "JanEggers" ***@***.***> wrote:
@MaximBalaganskiy <https://github.com/maximbalaganskiy> tried your code,
but it does not work for me. when i add a migration ef still creates
another MyUserId column like that: (from ModelSnapshot)
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.Property<string>("MyUserId");
b.HasKey("UserId", "RoleId");
b.HasIndex("MyUserId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
that is quite confusing as the modelbuilder in onmodelcreating seems to
pick it up correctly (OnModelCreating => builder.Model.DebugView)
EntityType: IdentityUserRole<string>
Properties:
RoleId (string) 0 0 -1 -1 -1
UserId (string) Required FK Index 1 1 0 -1 0
Foreign keys:
IdentityUserRole<string> {'UserId'} -> MaintenanceUser {'Id'} ToDependent: Roles
Annotations:
RelationshipDiscoveryConvention:NavigationCandidates: System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1361 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADimvKunXQnADyLp2ev73FaLk2tNHxrLks5sY9UggaJpZM4O3gPY>
.
|
This was working in version 1, I'm not quite a fan of EF, is this caused by the same issue that you are facing now @JanEggers ?
|
I had the same issue when there was no mapping setup for the navigation property. In this case EF creates the default mapping which has a different column name. |
@andrew-vandenbrink : jup that is exact my issue.
@MaximBalaganskiy: as i said i used your code and the mapping is still wrong |
@HaoK official feedback would be very welcome btw |
I haven't actually tried to create a new migration. Just with this change current code base works with the existing DB. It might be that the current snapshot is out of sync with models and this is preventing migration engine from working correctly. May be you need to tweak the snapshot to remove that mapping so that new run does not create a duplicate field. |
@JanEggers I guess it would make it faster, if you had a reproduction repo |
@JanEggers I just add what @MaximBalaganskiy suggest I don't run any ef migrations and it just works |
@andrew-vandenbrink if this workaround creates the identical snapshot then it's fine. I got an impression that the following migration failed. This could mean that models and current snapshot are out of sync. But again, can only be confirmed with the demo repo. |
@andrew-vandenbrink thx for the context i had my line before calling base.OnModelCreating(builder). |
@MaximBalaganskiy I brave enough to try migration...
and.... it still not working, i still need to put the workaround in ApplicationDbContext then it works again I think the issue is, in this case SQLite
equals to
which not working as you can see in the screenshot... (red colored message) therefore the workaround still need to be applied |
@HaoK is this workaround expected or I miss something? |
That mapping was copied from the previous identity context code for SQL
server. I suggest you checkout identity repo for 1.2 and copy the
respective line from sqlite related context
…On 17 Aug 2017 10:33 PM, "andrew-vandenbrink" ***@***.***> wrote:
@HaoK <https://github.com/haok> is this workaround expected or I miss
something?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1361 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADimvFH5HI6agk_yVfoc_c2B3MxODeyrks5sZDMJgaJpZM4O3gPY>
.
|
@ajcvickers I read the whole thing and didn't come to a conclusion, is it possible to have |
I'm burning so many hours trying to find a solution! I followed the instructions here, but when calling
I suggest this issue to be reopened. |
@weitzhandler I think it has to be |
I got it to work.
|
Any further official update on this? |
@YodasMyDad The issue is tracked here: dotnet/efcore#1368, please vote and comment. |
@weitzhandler I don't think that is the issue in this case, as identity in EF Core 2 has a AspNetUserRole table? This is the join table is it not? |
@YodasMyDad |
@weitzhandler |
I've just read through entire thread. If there's no official update, and no simple workaround, why the issue is closed? Maybe it should be reopened, @andrew-vandenbrink? |
@MaximBalaganskiy |
To people who say it doesn't work, it works, just read the thread properly... Once you make it work, you can make a query to show users and its role
|
@andrew-vandenbrink, @MaximBalaganskiy, thanks, guys! I just hadn't had any time lately to test your proposed solutions... I will certainly take a look! |
@prostakov There is way too much confusion regarding these navigation properties. See #1364 (comment) for a little more detail. The next step for MSFT should be to get these added to the default templates that are defined for new projects using individual user accounts. |
@waiseman That blog post is 4 years old and isn't for ASP.NET Identity Core. I suppose some concepts carry forward but it just adds to the confusion IMHO. |
@weitzhandler Thanks for that. I had to make a correction in order to add the nav property to the Role entity as well:
|
Hi there i think the problem is about where to place this line of code: when placed at the beginning of the method everything works fine, else it creates duplicate foreign keys. |
Here's my full solution (using default table names): https://stackoverflow.com/a/51005445/5392786 |
We're spending time on this... Dear Microsoft - don't make such changes plz. |
@andyfurniss4 About your stackoverflow, thanks a lot I finally succeeded to use UserRole after many months of try. But I had to modify your answer to make it work correctly, in the startup, to avoid this error: "Cannot create a DbSet for 'IdentityUserRole' because this type is not included in the model for the context", I changed your code to:
|
@ranouf Thanks for the update. I'm glad you managed to sort it out. I hadn't come across this issue personally but hopefully this will be helpful to anyone else who experiences it. |
f555a26
I see the change by making roles optional....
As I need to use the roles in IdentityUser, how can I do that?
Not using compatibility namespace I hope...
The text was updated successfully, but these errors were encountered: