0

I have followed the following and used ApplicationUser instead of creating my own. ApplicationUser comes from the default MVC 5 application:

http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx

This is my ApplicationUser code:

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; }
}

And this is my Field code:

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; }

    ...

But the following errors occur when i run 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.

Any ideas what i'm doing wrong?

Side note

I moved my ApplicationUser code into a seperate project called Domains. I included the Identity Nuget package to get the namespaces in it...

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233

1 Answers1

2

I ended up placing this:

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 } );

into my 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 } );
    }

I then had to run the migration, which then created a whole set of new tables... which i ended up keeping... I deleted the default tables.

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233