Entity framework EntityType';用户登录';没有定义键。定义此EntityType的键
我试图将我的ASP.NET标识类映射到我的数据库表,但我得到一个ModelValidationException,它说 “TestJam.UserLogins::EntityType'UserLogins'未定义键。请定义此EntityType的键。 UserLogins:EntityType:EntitySet“UserLogins”基于未定义键的类型“UserLogins”。“ 这是我的代码:Entity framework EntityType';用户登录';没有定义键。定义此EntityType的键,entity-framework,asp.net-identity,identity,ef-database-first,database-first,Entity Framework,Asp.net Identity,Identity,Ef Database First,Database First,我试图将我的ASP.NET标识类映射到我的数据库表,但我得到一个ModelValidationException,它说 “TestJam.UserLogins::EntityType'UserLogins'未定义键。请定义此EntityType的键。 UserLogins:EntityType:EntitySet“UserLogins”基于未定义键的类型“UserLogins”。“ 这是我的代码: public class TJUserRoles : IdentityUserRole<lo
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 });
知道为什么会这样吗
更新
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;
}
}
公共类tjuser:IdentityUser
{
公共字符串名称{get;set;}
公共bool被阻止{get;set;}
公共异步任务GenerateUserIdentityAsync(TJUserManager)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
我设置了一个scratch项目,它作为一个新数据库可以正常工作
你能确认你的TJUsers类实现了公共类吗。它是从IdentityUser继承的
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;
}
}