-1

Je suis en train de cartographier mes classes d'identité de ASP.NET à mes tables de base de données, mais je reçois un ModelValidationException qui ditEntityType 'UserLogins' n'a pas de clé définie. Définir la clé pour cette EntityType

« TestJam.UserLogins:: EntityType « UserLogins » n'a pas de clé définie Définir la clé pour cet EntityType UserLogins: EntityType: EntitySet 'UserLogins' est basé sur le type 'UserLogins' qui n'a pas de clés définies. "

Ceci est mon code:

public class TJUserRoles : IdentityUserRole<long> { } 

public class TJRoles : IdentityRole<long, TJUserRoles>, Services.IBaseProperties<long> 
{ 
    public bool Deleted { get; set; } 
    public long ModifiedBy { get; set; } 
    public DateTime ModifiedOn { get; set; } 
} 

public class TJUserClaims : IdentityUserClaim<long> { } 

public class TJUserLogins : IdentityUserLogin<long> { } 

public class TJDbContext : IdentityDbContext<TJUsers, TJRoles, long, TJUserLogins, TJUserRoles, TJUserClaims> 
{ 
    public TJDbContext() : base("name=TestJamMilkyWayIdentity") 
    { 
     Configuration.LazyLoadingEnabled = false; 
     Configuration.ProxyCreationEnabled = false; 
    } 

    public static TJDbContext Create() 
    { 
     return new TJDbContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<TJUserLogins>().Map(c => 
     { 
      c.ToTable("UserLogins"); 
      c.Properties(p => new 
      { 
       p.UserId, 
       p.LoginProvider, 
       p.ProviderKey 
      }); 
     }).HasKey(p => new { p.UserId, p.LoginProvider, p.ProviderKey }); 

J'ai essayé de la carte dans des ordres différents, mais rien n'a changé. Le

 modelBuilder.Entity<TJUsers>().ToTable("Users"); 
     modelBuilder.Entity<TJRoles>().ToTable("Roles"); 
     modelBuilder.Entity<TJUserRoles>().HasKey(k => new { k.UserId, k.RoleId }).ToTable("UserRoles"); 
     modelBuilder.Entity<TJUserClaims>().HasKey(k => new { k.Id }).ToTable("UserClaims"); 

     //modelBuilder.Entity<TJUsers>().HasMany(c => c.Logins).WithOptional().HasForeignKey(c => c.UserId); 
     //modelBuilder.Entity<TJUsers>().HasMany(c => c.Claims).WithOptional().HasForeignKey(c => c.UserId); 
     //modelBuilder.Entity<TJUsers>().HasMany(c => c.Roles).WithRequired().HasForeignKey(c => c.UserId); 


     modelBuilder.Entity<TJUsers>().Property(r => r.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<TJRoles>().Property(r => r.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<TJUserClaims>().Property(r => r.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     //modelBuilder.Entity<TJUserLogins>().Property(r => r.UserId).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
    } 
    public virtual DbSet<ComplexQuestions> ComplexQuestions { get; set; } 
    .... 
} 

public class TJUserStore : 
    UserStore<TJUsers, TJRoles, long, TJUserLogins, TJUserRoles, TJUserClaims>, 
    IUserStore<TJUsers, long>, 
    IDisposable 
{ 
    public TJUserStore(TJDbContext context) : base(context) { } 
} 

Mon tableau de code dans la base de données est:

CREATE TABLE [dbo].[UserLogins](
    [LoginProvider] [nvarchar](128) NOT NULL, 
    [ProviderKey] [nvarchar](128) NOT NULL, 
    [UserId] [bigint] NOT NULL, 
CONSTRAINT [PK_UserLogins] PRIMARY KEY CLUSTERED 
(
    [LoginProvider] ASC, 
    [ProviderKey] ASC, 
    [UserId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[UserLogins] WITH CHECK ADD CONSTRAINT [FK_dbo.UserLogins_dbo.Users_UserId] FOREIGN KEY([UserId]) 
REFERENCES [dbo].[Users] ([Id]) 
ON DELETE CASCADE 
GO 

Toute idée pourquoi est-ce qui se passe?

Mise à jour

public class TJUsers : IdentityUser<long, TJUserLogins, TJUserRoles, TJUserClaims> 
{ 
    public string Name { get; set; } 
    public bool IsBlocked { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(TJUserManager manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

Répondre

0

Je mis en place un projet scratch et il fonctionne bien comme une nouvelle base de données.

Pouvez-vous confirmer votre TJUsers classe implémente public class <long,TJUserLogins,TJUserRoles, TJUserClaims>

+0

Oui, il le fait. Il est hérité de IdentityUser – Totati