将表与 Microsoft 身份用户相关联 - 没有键定义错误
Relating tables to a Microsoft Identity User - has no key defined errors
我遵循以下内容并使用 ApplicationUser
而不是创建我自己的。 ApplicationUser
来自默认 MVC 5 应用程序:
这是我的 ApplicationUser
代码:
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
代码移到了一个名为 Domains 的单独项目中。我包含了 Identity Nuget 包以在其中获取命名空间...
我最后放置了这个:
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 } );
}
然后我不得不 运行 迁移,然后创建了一整套新表...我最终保留...我删除了默认表。
我遵循以下内容并使用 ApplicationUser
而不是创建我自己的。 ApplicationUser
来自默认 MVC 5 应用程序:
这是我的 ApplicationUser
代码:
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
代码移到了一个名为 Domains 的单独项目中。我包含了 Identity Nuget 包以在其中获取命名空间...
我最后放置了这个:
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 } );
}
然后我不得不 运行 迁移,然后创建了一整套新表...我最终保留...我删除了默认表。