-1
我想我的ASP.NET身份類映射到我的數據庫表中的關鍵,但我得到一個ModelValidationException,說EntityType'用戶登錄'沒有定義密鑰。定義此的EntityType
「TestJam.UserLogins:的EntityType‘UserLogins’沒有定義的關鍵。定義此EntityType的密鑰 UserLogins:EntityType:EntitySet'UserLogins'基於沒有定義密鑰的'UserLogins'類型。
這是我的代碼:
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 });
我嘗試以不同的順序來圖,但沒有任何改變。 的
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) { }
}
我的表在數據庫的代碼是:
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
任何想法,爲什麼會出現這種情況?
更新
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;
}
}
是的,它的作用。它從IdentityUser繼承 – Totati