0
People created the whole structure Identity to work with INT instead of GUID. Using Database First and included in the database, with a create script, the modified Identity tables (I only changed Aspnetusers).
but every time I call:
userManager.Users.ToList();
I get the following error:
The Entity types 'Customrole' and 'Aspnetroles' cannot share table 'Aspnetroles' because they are not in the same type Hierarchy or do not have a Valid one to one Foreign key Relationship with matching Primary Keys between them
I’ll put some snippets of the code below... but I don’t understand My Custonrole just inherits Identityrole and my Aspnetroles is untouched equal to that which was automatically created in the first user registration of a reference application. I even have this reference application pointing to the same bank and working without this error. As far as I know 2 (reference application and my official application) are with identical Identity settings.
POCO entity generated for Aspnetroles:
namespace EFBcoDadosSQL
{
using System;
using System.Collections.Generic;
public partial class AspNetRoles
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetRoles()
{
this.AspNetUsers = new HashSet<AspNetUsers>();
}
public int Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUsers> AspNetUsers { get; set; }
}
}
My Custonrole to work with INT:
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
My Rolemanager:
public class ApplicationRoleManager : RoleManager<CustomRole, int>
{
public ApplicationRoleManager(IRoleStore<CustomRole, int> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore<CustomRole, int, CustomUserRole>(new GPSdEntitiesIdentity()));
}
}
My Identity Context and Applicationuser:
public class CustonUsers : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<CustonUsers, int> manager)
{
// Observe que o authenticationType deve corresponder àquele definido em CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Adicionar declarações de usuário personalizado aqui
return userIdentity;
}
public string LOGIN { get; set; }
[ForeignKey("STATUS_REGISTROS")]
public int IDSTATUSREGISTRO { get; set; }
public virtual STATUS_REGISTROS STATUS_REGISTROS { get; set; }
}
public class GPSdEntitiesIdentity : IdentityDbContext<CustonUsers, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public GPSdEntitiesIdentity()
: base("IdentityConnection")
{
}
public static GPSdEntitiesIdentity Create()
{
return new GPSdEntitiesIdentity();
}
public virtual DbSet<STATUS_REGISTROS> STATUS_REGISTROS { get; set; }
}
If you need any more information just ask. I’m already racking my brain with this setup for days... And I only suggested adopting Entity and Identity because it would be simpler than pure ADO. But it’s been much more complicated
Note: I did following these excellent tutorials: https://danieleagle.com/2014/05/setting-up-asp-net-identity-framework-2-0-with-database-first-vs2013-update-2-spa-template/
Friend, make sure this video doesn’t help you https://www.youtube.com/watch?v=dWmT9R_ZthY
– Pablo Tondolo de Vargas
I saw there and this part of going to int I did the same as him, with the difference that I worked with databasefirst.
– AlamBique
@Pablotondolodevargas discovered that the problem is a little different than I thought... There seems to be a conflict between the Identity tables and the tables in my context. I explained it better here:https://answall.com/questions/257634/erro-quando-identitydbcontextcontextusa-entidades-edmx-do-contextual
– AlamBique