我已按照以下方式使用ApplicationUser
而不是创建自己的ApplicationUser
。 ApplicationUser
来自默认的MVC 5应用程序:
这是我的public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync( UserManager<ApplicationUser> 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;
}
public virtual ICollection<Field> Fields { get; set; }
}
代码:
Field
这是我的public class Field
{
[Key]
public int FieldID { get; set; }
// [ForeignKey( "Id" )] // Not sure if i needed this but neither worked
public virtual ApplicationUser CreatedBy { get; set; }
...
代码:
PM> Add-Migration AddedCreatedByToFields
但是当我运行One or more validation errors were detected during model generation:
FieldApplication.Domain.Concrete.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
FieldApplication.Domain.Concrete.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
时会出现以下错误:
ApplicationUser
任何想法我做错了什么?
旁注
我将{{1}}代码移到了一个名为Domains的独立项目中。我包含了Identity Nuget包来获取其中的命名空间......
答案 0 :(得分:2)
我最后放置了这个:
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>( l => l.UserId );
modelBuilder.Entity<IdentityRole>().HasKey<string>( r => r.Id );
modelBuilder.Entity<IdentityUserRole>().HasKey( r => new { r.RoleId, r.UserId } );
进入我的EFDbContext:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
// base.OnModelCreating( modelBuilder );
Precision.ConfigureModelBuilder(modelBuilder);
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>( l => l.UserId );
modelBuilder.Entity<IdentityRole>().HasKey<string>( r => r.Id );
modelBuilder.Entity<IdentityUserRole>().HasKey( r => new { r.RoleId, r.UserId } );
}
然后我必须运行迁移,然后创建了一整套新表...我最终保留...我删除了默认表。