2017-02-18 6 views
1

내 첫 번째 질문에 stackoverflow.com! 두 개의 필드에 일치하는 요구, 내게 몇 가지 테스트 데이터로 개발 데이터베이스를 시드 노력하고 있어요 :)여러 필드에서 AddOrUpdate 일치를 사용하여 ASP.NET MVC를 시드하는 동안`오직이 기본 유형 또는 열거 형이이 컨텍스트에서 지원됩니다. '오류

설명 행운을 빌어, 그리고

How to seed data with AddOrUpdate with a complex key in EF 4.3 내가 Only primitive types or enumeration types are supported in this context. 오류 as mentioned in the comments by lukyer를 얻을 여기 해결 문제가 발생했습니다.

그러나 외장 키 특성 및 필요한 특성을 모델의 속성에 추가 했음에도 불구하고 오류를 제거 할 확실한 방법을 찾을 수 없습니다.

데이터베이스를 성공적으로 시드하는 방법에 대한 아이디어는 감사의 말로 받아 볼 수 있습니다. 사용자 ([email protected])과 기관 (극비 설립)을 추가 한 후 configuration.cs에서 마이그레이션하는 코드를

public class User { 
    public string ID { get; set; } 
    public string Email {get; set; } 
} 

public class Institution { 
    public Guid ID { get; set;} 
    public string Name {get; set; } 
} 

public class UserInstitutionAssociation { 
    public Guid ID { get; set; } 
    public virtual User User { get; set; } 
    public virtual Institution Institution {get; set;} 
} 

을 그리고 다음과 같이

모델의 중요한 부분이다. 그런 다음 사용자와 시설간에 연관을 추가하려고 시도합니다.이 둘에 대한 연관성이 아직 존재하지 않는 경우에만 추가해야합니다. 내가 Lukyer을 이해

//Add User to Database 
context.Users.AddOrUpdate(u => u.Email, 
    new User { 
     Email = "[email protected]" 
    }); 
context.SaveContext(); 

//Add Institution to Database 
context.Institutions.AddOrUpdate(u => u.Name, 
    new Institution { 
     Name = "Top Secret Establishment" 
    }); 
context.SaveContext(); 

//Add Association between User and Institution 
context.InstitutionUserAssociations.AddOrUpdate(u => new { u.User, u.Institution }, 
    new InstitutionUserAssociation 
    { 
     User = context.Users.Single(x => x.Email=="[email protected]"), 
     Institution = context.Institutions.Single(x => x.Name=="Top Secret Establishment") 
    } 
); 
context.SaveContext(); 

내가 한마디로 여기 How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

설명 된 모든 솔루션을 포함하여, 제안하는 여러 가지 방법으로 모델을 변경 시도, 나는 성공없이 다음의 모든 조합을 시도했다 :

public class UserInstitutionAssociation { 
    public Guid ID { get; set; } 
    [Required] 
    [ForeignKey("UserID")] 
    public virtual User User { get; set; } 
    [Required] 
    [ForeignKey("InstitutionID")] 
    public virtual Institution Institution {get; set;} 
} 

public class UserInstitutionAssociation { 
    public Guid ID { get; set; } 
    [Required] 
    [ForeignKey("User")] 
    public string UserID {get; set; } 
    public virtual User User { get; set; } 

    [Required] 
    [ForeignKey("Institution")] 
    public Guid InstitutionID { get; set; } 
    public virtual Institution Institution {get; set;} 
} 

답변

1

해결되었습니다. LINQ 쿼리의 일부로 포함 된 클래스 때문에 발생했습니다. x.User와 x.Institution이 모두 복잡한 클래스이기 때문에 x => new { x.User, x.Institution}을 사용할 수 없습니다. 대신 기본 또는 기본 문자열 형식 (예 : 문자열 또는 GUID)이어야합니다. 이 방법으로 변경되었습니다 AddOrUpdate(x => new {x.UserID, x.InstitutionID}, Associations). 그런 다음 LINQ에서 쿼리 할 수있는 UserID 및 InstitutionID라는 속성이 포함되도록 모델을 변경해야했습니다. 다음과 같이

public class UserInstitutionAssociation { 
    public Guid ID { get; set; } 

    [Required] 
    [ForeignKey("User")] 
    public string UserID {get; set; } 
    public virtual User User { get; set; } 

    [Required] 
    [ForeignKey("Institution")] 
    public Guid InstitutionID { get; set; } 
    public virtual Institution Institution {get; set;} 
} 

는 그 다음 AddOrUpdate 호출을 조정 :

//Add Association between User and Institution 
context.InstitutionUserAssociations.AddOrUpdate(u => new { u.UserID, u.InstitutionID }, 
    new InstitutionUserAssociation 
    { 
     User = context.Users.Single(x => x.Email=="[email protected]"), 
     Institution = context.Institutions.Single(x => x.Name=="Top Secret Establishment") 
    } 
); 
을 그 특성이 사용자 ID 및 InstitutionID뿐만 아니라 사용자 및 I라는 포함 있도록 다음과 같이

은 내가 UserInstitutionAssociation 코드를 조정