0

私はaspコア2.0で言語辞書を開発しています。どのように 私のデータベースを適切に設計するのだろうか。私は同様の質問に遭遇した:How to design a database for translation dictionary?1つのEntity Frameworkコアテーブルへの2つの外部キー

私は、次の写真のようなデータベースを作成することにしました:

database structure that i wanna realize in ef core

しかし、私は、エンティティフレームワークのコア2.0でこの構造を実現する方法がわかりません。私は、移行を追加しようとする

Wordのエンティティ

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(); 
     }); 
関連する問題