Dear Csharpers,
I am customizing the default identity in a MVC asp.net core 6.0 project. I wanted to change the types of the Ids, thats why I inherited and have overriden the default IdentityUser, IdentityRoles na IdentityUserRoles as follows:
public class User : IdentityUser<Guid>
{
public override Guid Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
......
......
//public ICollection<UserRole> UserRoles { get; set; }
#nullable enable
public ICollection<Listing>? Listings { get; set; }
}
public class Role : IdentityRole<Guid>
{
public override Guid Id { get; set; }
public string Discriminator { get; set; }
}
public class UserRole : IdentityUserRole<Guid>
{
public Guid Id {get; set;}
public override Guid UserId { get; set; }
public override Guid RoleId { get; set; }
}
Then I start the application and check, if the Database is empty => I seed an admin - create an user with new Guid for Id, then create and save Role with new Guid for RoleId, but after I try to create and add UserRole with the folowing code:
var userRole = new UserRole
{
Id = Guid.NewGuid(),
UserId = admin.Id,
RoleId = role.Id
};
....there comes this error:
"System.InvalidOperationException: 'The value of 'IdentityUserRole<Guid>.UserId' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.'"
Can anyone help me to solve this issue?