I am developing language dictionary with asp core 2.0. I wonder how to properly design my database. I came across similar question: How to design a database for translation dictionary?.
I decided to create a database like in the following picture:
database structure that i wanna realize in ef core
But i do not know how to realize this structure with entity framework core 2.0.
Word entity
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; }
}
Translation entity
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; }
}
fluent API
modelBuilder.Entity<Translation>()
.HasOne(w => w.Words)
.WithMany(m => m.Translations)
.HasForeignKey(fk => new { fk.WordId1, fk.WordId2 });
when i try to add a migration, i get the error:
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.