2016-12-06 4 views
0

У меня есть приложение ASP.NET MVC с двумя отдельными базами данных. Первая база данных контекст стандарт идентификацииОшибка проверки модели MVC MVC MVC от Identity

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

второй контекст

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

}

Я успешно активировали миграции для TestModel и попробуйте запустить

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

, что приводит к ошибке

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. 

Мне не удалось найти что-либо, связанное с этой проблемой, и оцените любую помощь здесь. Спасибо.

+0

Возможный дубликат: http://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit –

+0

Спасибо за ссылку, но она не разрешать идиоты. У меня нет проблем с запуском миграции в ApplicationDbContext, где принадлежит Identity. Проблема возникает, когда я создаю миграцию во втором контексте, который предполагает отсутствие связей Identity и в этой модели нет классов «Models.Tests.IdentityUserLogin» и «Models.Tests.IdentityUserRole». Как-то EF получает его из ApplicationDbContext, и я не могу понять, как его исправить. Спасибо. – timk

+0

У любой из моделей во втором контексте есть ссылка на модели идентичности? Кроме того, [вы включили миграции для каждого контекста?] (Http://stackoverflow.com/questions/21537558/multiple-db-contexts-in-the-same-db-and-application-in-ef-6-and -code-first-migra) –

ответ

0

Проверьте базу данных основного и иностранного ключей в обеих базе данных. Первое ограничение на восстановление, связав обе базы данных по отдельности. Для миграции вы также можете использовать репликацию. Надеюсь, этот намек вам поможет.