我正在尝试将我的ASP.NET标识类映射到我的数据库表,但是我得到了一个说明
的ModelValidationException“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;
}
}
答案 0 :(得分:0)
我设置了一个临时项目,它可以作为一个新的数据库工作。
您能否确认您的TJUsers
课程实施公共课程<long,TJUserLogins,TJUserRoles, TJUserClaims>