0

I have ASP.NET MVC application with 2 separate databases. The first database is standard Identity context

  public class ApplicationUser : IdentityUser  {
    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager) {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)  {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
    public ApplicationDbContext()
        : base("ApplicationConnection", throwIfV1Schema: false) { }

    public static ApplicationDbContext Create() {
        return new ApplicationDbContext();
    }
}

second context

 public class TestModel : DbContext {
    public TestModel()
        : base("Connection") {
    }
    public virtual DbSet<A> As { get; set; }
    public virtual DbSet<B> Bs { get; set; }
    public virtual DbSet<C> Cs { get; set; }

}

I successfully enabled migration for TestModel and try to run

add-migration Initial -ConfigurationTypeName project.Migrations.TestMigrations.Configuration

which results in the error

One or more validation errors were detected during model generation:

project.Models.Tests.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

project.Models.Tests.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 was not able to find anything related to this problem and appreciate any help here. Thank you.

Hamed Moghadasi
  • 1,523
  • 2
  • 16
  • 34
timk
  • 3
  • 2
  • Possible dup: http://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit – Steve Greene Dec 06 '16 at 20:59
  • Thank you for the link but it does not resolve the iddue. I have no problem running migrations on ApplicationDbContext where Identity belongs. The problem occurs when I create migration on second context which suppose to have no connections Identity and there are no classes "Models.Tests.IdentityUserLogin" and "Models.Tests.IdentityUserRole" in this model. Somehow EF gets it from ApplicationDbContext and I cannot figure out how to fix it. Thank you. – timk Dec 07 '16 at 14:38
  • Do any of the models in the second context have a reference to the Identity Models? Also, [have you enabled migrations for each context?](http://stackoverflow.com/questions/21537558/multiple-db-contexts-in-the-same-db-and-application-in-ef-6-and-code-first-migra) – Steve Greene Dec 07 '16 at 17:33
  • None. Classes A, B, and C in the second context just regular classes like – timk Dec 07 '16 at 21:17
  • public int ID {get; set;} , public string Name {get;set;} and nothing more. – timk Dec 07 '16 at 21:19
  • Each context has own migration. – timk Dec 07 '16 at 21:21

1 Answers1

0

Please check the primary and foreigner key constraint in both database. First repair identity constraint by connecting both database individually. For Migration you can use replication as well. Hope this hint will help you.