C# 将表与Microsoft Identity用户关联-没有键定义的错误

C# 将表与Microsoft Identity用户关联-没有键定义的错误,c#,asp.net,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net,Asp.net Mvc 5,Asp.net Identity 2,我遵循以下步骤,使用了ApplicationUser,而不是创建自己的应用程序应用程序用户来自默认的MVC 5应用程序: 这是我的应用程序用户代码: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync( UserManager<ApplicationUser> manager ) {

我遵循以下步骤,使用了
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; }
}
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>addmigration 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 } );
modelBuilder.Entity().HasKey(l=>l.UserId);
modelBuilder.Entity().HasKey(r=>r.Id);
modelBuilder.Entity().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 } );
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//基于模型创建(modelBuilder);
精度。配置modelBuilder(modelBuilder);
modelBuilder.Entity().HasKey(l=>l.UserId);
modelBuilder.Entity().HasKey(r=>r.Id);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.UserId});
}

然后我不得不运行迁移,然后创建了一整套新表。。。而我最终还是。。。我删除了默认表。

没有人应答,因为代码似乎正确吗?如果是这样,这将帮助我找到问题:)