Closed unencode closed 4 years ago
@unencode Hi I'm taking a look into this. The issue is with the identity context and not the EFCoreStore context - so that will help narrow it down. Can you post your AppUser class?
I think I see the problem. In AddIdentity
you pass MultiTenantUser
, but later in the ApplicationDbContext
you designate AppUser
as the user entity class:
public class ApplicationDbContext : MultiTenantIdentityDbContext<AppUser>
If you just want the default Identity stuff you can just use MultiTenantIdentityUser
everywhere and you don't need AppUser
. If you add your own properties to the user model, you'd do so in AppUser
(which inherits from MultitenantIdentityUser
) so then make sure when you add identity you specify AppUser
as the user entity.
Let me know how it goes.
Thanks for your help! My AppUser class is posted below, as you can see it's empty. I am going to work through your second answer in just a bit here and report back. Thanks!
[MultiTenant]
public class AppUser : MultiTenantIdentityUser
{
}
Thanks again. As you suggested I removed AppUser as it's not really being used.
So the only line of code that changed is class declaration for ApplicationDbContext which now reads:
public class ApplicationDbContext : MultiTenantIdentityDbContext<MultiTenantIdentityUser>
That's where I see the other exception I mentioned which also happens at _userManager.CreateAsync in Register.cshtml.cs. Here's the exception:
System.InvalidOperationException: Unable to track an entity of type 'MultiTenantIdentityUser' because primary key property 'Id' is null.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableKeyIdentityMap`1.Add(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.CreateAsync(TUser user, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.UserManager`1.CreateAsync(TUser user)
at Microsoft.AspNetCore.Identity.UserManager`1.CreateAsync(TUser user, String password)
I'm going to keep working on it. If you have any insight or ideas I would super appreciate it. At first I was thinking I had my stores mixed up or something.
Thanks!
In testing things out, if I add my own Id things work. From Register.cshtml.cs OnPostAsync:
var user = new MultiTenantIdentityUser { UserName = Input.Email, Email = Input.Email };
ADDED THIS>>> user.Id = Guid.NewGuid().ToString();
var result = await _userManager.CreateAsync(user, Input.Password);
After doing this Finbuckle adds the TenantId properly to Identity and custom entities and the host routing works.
I'm glad you got it working. FYI MultiTenantIdentityDbContext
derives from MultiTenantIdentityDbContext<MultiTenantIdentityUser
> so your app db context can just derive from MultiTenantIdentityDbContext
if you want to simplify--no need to though.
I thought that the user ID would automatically create a GUID for you -- does regular Identity do that? It has been a while I will want to check on that. Ideally you wouldn't have to set the user ID yourself.
Great, thank you, I will simplify it.
That's right, normally regular Identity's UserManager will create the user and it will be assigned a GUID.
I found out why the Id null issue is happening--I will have a fix in the next release.
I found out why the Id null issue is happening--I will have a fix in the next release.
Great! I'm looking forward to the fix and am super curious about it. Thanks!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I have an ASP.NET Core 3.0 application using Razor Pages for Identity and I'm getting stuck trying to register a new user receiving this exception
Perhaps it's not wired up properly. At the moment it has two DBContexts and two databases.
ConfigureServices:
ApplicationDBContext:
TenantDbContext:
In Register.cshtml.cs excerpt:
I've also reconfigured it where it looks like it will add the user at _userManager.CreateAsync but then crashes saying the user's Id is null.