0

I have read all at stackoverflow but cannot manage my error:

 public class Album
  {
    public int AlbumId { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }
    public virtual ApplicationUser User { get; set; }
  }

  public class ApplicationUser : IdentityUser
    {
    public virtual ICollection<Album> Albums { get; set; }
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Album> Albums { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        Configuration.LazyLoadingEnabled = true;
        Configuration.ProxyCreationEnabled = false;
        Database.SetInitializer<ApplicationDbContext>(new CreateDatabaseIfNotExists<ApplicationDbContext>());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

And got:

One or more validation errors were detected during model generation:

photoManager.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
photoManager.Models.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.

I am stuck!!! So Please HELP. What is wrong?

Lagoda Denis
  • 235
  • 5
  • 13
  • Can you please share the definition of photoManager.Models.IdentityUserLogin and photoManager.Models.IdentityUserRole? – Parth Shah May 18 '15 at 07:54
  • Seems like ASP.NET MVC Identity is not able to identify the Primary Key(Id) of IdentityUserLogins & IdentityUserRoles Tables. I don't know but may be in SimpleMembershipInitializer 'WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true)' changing your params can help. – Krishna May 18 '15 at 08:05
  • IdentityUserLogin and IdentityUserRole are the default Identity Models - I do not want define my own - at least now. And I do not know why it looks for "photoManager.Models.IdentityUserLogin" reference. It is the outbox MVC solution - I just added my Album table and try to add ApplicationUser field – Lagoda Denis May 18 '15 at 08:40

1 Answers1

0

Two things need attention

  1. Check the DbContext you are using. It may be the case that you can have more than one context and you're using a context which has not been initialized properly

  2. Try updating the OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder 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 });
    }
    

You can refer to the following if you have not already

1.Modify DbContext

2.Change ModelBinding

3.Change ModelBinding(Tricky)

Community
  • 1
  • 1
Krishna
  • 5,194
  • 2
  • 28
  • 33
  • 1. I use IdentityDbContext 2. I have protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } - it means that all is defined in IdentityDbContext – Lagoda Denis May 18 '15 at 09:48
  • Thank you all guys. I checked my code twice and found a GHOST old Dbcontext that I used before I merged it with ApplicationDbContext. And some classes used it. My stupid mistake!!! – Lagoda Denis May 18 '15 at 09:58
  • Please provide detailed answer, this is something what im struggling too – Jon Koivula May 18 '15 at 10:05
  • Told you that their must be some issue with your DbContext. :) – Krishna May 18 '15 at 10:05
  • JON >> search your code for other "old" DbContext - and remove or at least comment that. And you will see does any class use that old DbContext or not. – Lagoda Denis May 18 '15 at 10:20