0

asp core 2.0으로 언어 사전을 개발 중입니다. 내 데이터베이스를 올바르게 디자인하는 방법에 대해 궁금합니다. 비슷한 질문을 보니 : How to design a database for translation dictionary?. 하나의 Entity Framework 핵심 테이블에 두 개의 외래 키

나는 다음 그림에서와 같이 데이터베이스를 만들하기로 결정

database structure that i wanna realize in ef core

하지만 난 엔티티 프레임 워크 코어 2.0이 구조를 실현하는 방법을 모르겠어요.

워드 엔티티

public class Word 
    { 
     public Word() 
     { 
      Translations = new HashSet<Translation>(); 
     } 
     [Key] 
     public Guid WordId { get; set; } 
     [Required] 
     public Guid LangCodeId { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
     "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Translation> Translations { get; set; } 


} 

번역 엔티티

public class Translation 
{ 
    [Key] 
    public Guid TranslationId { get; set; } 
    public Guid WordId1 { get; set; } 
    public Guid WordId2 { get; set; } 

    //[ForeignKey("WordId1,WordId2")] 
    public Word Words { get; set; } 

} 

유창 API

modelBuilder.Entity<Translation>() 
      .HasOne(w => w.Words) 
      .WithMany(m => m.Translations) 
      .HasForeignKey(fk => new { fk.WordId1, fk.WordId2 }); 

내가 마이그레이션을 추가하려고하면, 나는 오류 얻을 :

The relationship from 'Translation.Words' to 'Word.Translations' with 
    foreign key properties {'WordId1' : Guid, 'WordId2' : Guid} cannot target 
    the primary key {'WordId' : Guid} because it is not compatible. Configure 
    a principal key or a set of compatible foreign key properties for this 
    relationship. 

답변

0

나는 해결책을 발견

public partial class Translation 
{ 
    public Guid DerivedId { get; set; } 
    public Guid? Word1 { get; set; } 
    public Guid? Word2 { get; set; } 

    public Word Word1Navigation { get; set; } 
    public Word Word2Navigation { get; set; } 
} 



public partial class Word 
{ 
    public Word() 
    { 
     TranslationWord1Navigation = new HashSet<Translation>(); 
     TranslationWord2Navigation = new HashSet<Translation>(); 
    } 

    public Guid Id { get; set; } 

    public ICollection<Translation> TranslationWord1Navigation { get; set; } 
    public ICollection<Translation> TranslationWord2Navigation { get; set; } 
} 

유창함 API

 modelBuilder.Entity<Translation>(entity => 
     { 
      entity.HasKey(e => e.DerivedId); 

      entity.Property(e => e.DerivedId).ValueGeneratedNever(); 

      entity.HasOne(d => d.Word1Navigation) 
       .WithMany(p => p.TranslationWord1Navigation) 
       .HasForeignKey(d => d.Word1) 
      .HasConstraintName("FK__DerivedTa__Word1__5EBF139D"); 

      entity.HasOne(d => d.Word2Navigation) 
       .WithMany(p => p.TranslationWord2Navigation) 
       .HasForeignKey(d => d.Word2) 
       .HasConstraintName("FK__DerivedTa__Word2__5FB337D6"); 
     }); 

     modelBuilder.Entity<Word>(entity => 
     { 
      entity.Property(e => e.Id) 
       .HasColumnName("id") 
       .ValueGeneratedNever(); 
     }); 
관련 문제