2016-09-14 1 views
2

엔티티 프레임 워크에서 ApplicationUser과 내 자신의 클래스 사이에 1 : 1 관계가 필요합니다.엔티티 프레임 워크에서 1 : 1 관계

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

    /*Realations*/ 

    public virtual ICollection<Comment> Comments { get; set; } 
    public virtual Posts Post { get; set; } 
} 


public class Posts : System.Object 
{ 
    public Posts() 
    { 
     this.PostDate = DateTime.Now; 
     this.PostViews = 0; 
     this.PostPic = "d.jpg"; 
    } 

    [Key] 
    public int PostID { get; set; } 

    public string PostName { get; set; } 
    public string PostSummery { get; set; } 
    public string PostDesc { get; set; } 
    public string PostPic { get; set; } 
    public DateTime PostDate { get; set; } 
    public int PostViews { get; set; } 
    public string postMetaKeys { get; set; } 
    public string PostMetaDesc { get; set; } 


    public string UserId { get; set; } 
    [Key, ForeignKey("UserId")] 
    public virtual ApplicationUser ApplicationUser { get; set; } 


    public int CategoryID { get; set; } 
    [Key, ForeignKey("CategoryID")] 
    public virtual Categories Category { get; set; } 

    public virtual ICollection<Comment> commnets {get; set;} 
} 

하지만 Nuget 콘솔 내부의 명령 "추가 마이그레이션 관계"를 쓸 때 나는 예외를 얻고있다 :

나는이 작업을 수행. 유형 'FinalKaminet.Models.ApplicationUser'및 'Models.Posts'간의 관계의 주 단부를 결정할

없습니다. 이 연관의 주 끝 부분 인 은 유동적 인 API 또는 데이터 주석 중 하나 인 을 사용하여 명시 적으로 구성되어야합니다.

나는 또한 IdentityModels 내부 코드 아래에 추가 할 수 있지만 다른 오류가 나타났다

:

ApplicationUser_Post_Target :

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 


     modelBuilder.Entity<ApplicationUser>() 
     .HasOptional(f => f.Post) 
     .WithRequired(s => s.ApplicationUser); 
    } 

하나 이상의 유효성 검사 오류가 모델 생성 중에 검색된는 : 다중성은 아니다 역할 'ApplicationUser_Post'에 'ApplicationUser_Post_Target'역할 에서 유효합니다. 종속 역할 속성이 주요 속성이 아니기 때문에 종속 역할의 다중도가 상한선은 '*'여야합니다.

무엇이 잘못 되었나요?

+0

표시되는 내용 * 다른 오류 *? –

+0

@AdilMammadov, 업데이트 된 질문. – Farzaneh

+0

'IdentityUser'에는'string' 키가 있습니다. 문제가 될 수 있습니까? –

답변

1

사용자와 게시물간에 1 대 1 관계가 필요합니까? 사용자는 게시 할 수있는 게시물이 하나뿐입니다.

어쨌든 EF (최소 6)에서 동일한 PK를 공유하는 두 엔티티간에 1 : 1 관계를 설정할 수 있습니다. PK가 FK입니다. 따라서 posts의 PK를 문자열로 설정해야합니다.

그렇지 않으면 1 대 * 관계입니다.

+0

수정하십시오. 그것은 일종의 FK 1 대 1 연결을 사용하는 것이 가능하지만 단방향 일 때만 가능합니다. 참조 : [공유 기본 키 연결] (http://weblogs.asp.net/manavi/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations) vs [One 1 대 외래 키 협회 (http://weblogs.asp.net/manavi/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations)). –

0

Posts 클래스의 UserId 속성에 [Required] 속성을 추가하십시오.

기본적으로 문자열 속성은 null 가능하지만 외부 키이므로 관계를 필수로 설정하려면 속성을 null이 아닌 것으로 설정해야합니다. [Required] 속성을 추가하여 수행 할 수 있습니다 .

+0

'[Required]'를'UserId'에 추가하면 두 번째 오류가 발생합니다. – Farzaneh