13

내 MVC5 응용 프로그램에 프로필/회원 정보를 추가하고 구성 매핑을 추가하려고합니다.asp.net MVC5 EF6에서 유창한 API를 사용하여지도 테이블?

나는 다음과 같은 오류 메시지가 :

my.Models.IdentityUserLogin는 : EntityType 'IdentityUserLogin는'정의 된 키가 없습니다. 이 EntityType의 키를 정의하십시오.

my.Models.IdentityUserRole :: EntityType 'IdentityUserRole'에는 키가 없습니다. 이 정의되어 있습니다. 이 EntityType의 키를 정의하십시오.

IdentityUserLogins : EntityType : EntitySet 'IdentityUserLogins'는 정의 된 키가없는 'IdentityUserLogin'유형을 기준으로 입니다.

IdentityUserRoles : EntityType : EntitySet 'IdentityUserRoles'는 정의 된 키가없는 'IdentityUserRole'유형의 을 기반으로합니다.

public class ApplicationUser : IdentityUser 
{ 
    public string City { get; set; } 
    public string Discriminator { get; set; } 

    public string Address { get; set; }  
} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new ApplicationUserConfiguration());   
    } 
} 

답변

18

전화 base.OnModelCreating(modelBuilder) 후 구성했다.

+2

이 나던 work..try이 그것을 호출하는 경우 before the thefiguredaded –

+1

'IdentityDbContext '가 사용되고,'OnModelCreating'가 오버라이드되어 있고,'IdentityUs를 수동으로 설정하지 않으면'base.OnModelCreating (modelBuilder);가 호출되어야합니다. erLogin','IdentityUserRole','IdentityUserClaim' 속성이나 유창한 API를 가지고 있습니다. –

22

base.OnModelCreating(modelBuilder)을 호출해도 문제가 해결되지 않았습니다.

Microsoft.AspNet.Identity.EntityFramework의 동작은 VS2013- 미리보기, VS2013-RC 및 VS2013-RTM에서 서로 다른 것으로 보입니다. RTM 버전을 사용하고 있습니다. 내가 AspNet.Identity.EntityFramework에 작업이 진행중인 것 같다

public class ApplicationUser : IdentityUser 
{ 
    public string DisplayName { get; set; } 
} 


public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() : base("DefaultConnection") { } 

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

(Configuring/Mapping Properties and Types with the Fluent API 참조) :

IdentityUser에서 상속 후 나는 그것이 작동하도록 모델의 다른 모든 기본 키를 다시했다 이것은 고정 될 것입니다 (?)

+0

나는이 문제를 해결하기 위해 똑같은 일을해야만했다. EF가 엔티티 ID를 자동으로 선택한다고 생각 했나요? –

+2

이 모든 것이 저를 위해 해결되었습니다!많은 감사합니다 - 대답으로 표시해야합니다 :) – Darren

+0

나는 비슷한 문제가 있었고이 솔루션을 시도하고 문제를 해결 생각하지만 나는 IdentityUsers와 같은 데이터베이스에 몇 가지 여분의 테이블을 받고 있고, 나는 여전히 AspNetUsers 테이블이 있습니다. 내가 뭘 잘못 했니? – lawphotog

0

OnModelCreating 방법을 사용하여 다음 단계를 수행하고 각각을 적용한 후에 테스트하십시오.

  1. 규칙 충돌을 방지하려면 Context이 있는지 확인하십시오.
  2. (우선) 언급 방법 내부
  3. 전화 base.OnModelCreating(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 }); 
관련 문제