The presented database model lacks one important (from relational standpoint) part - the unique principal entity representing the one side of the many-to-one relationships for string.id
, elem.titleId
, elem.BriefId
and similar.
Without that part the database model cannot use/enforce foreign key relationships, and these are essential for "convenient" EF relationship mapping.
So the minimal required modification is to introduce that entity/table, let call it for instance stringTable
:
CREATE TABLE stringTable
(
id INTEGER NOT NULL PRIMARY KEY
);
For existing database it should be populated with distinct id
values from string
table.
Now you can introduce FK relationships:
CREATE TABLE string
(
id INTEGER NOT NULL CONSTRAINT FOREIGN KEY REFERENCES stringTable(id) ON DELETE CASCADE,
locale TEXT NOT NULL,
text TEXT NOT NULL,
CONSTRAINT PK_string PRIMARY KEY (id, locale)
);
CREATE TABLE elem
(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
titleId INTEGER NOT NULL CONSTRAINT FOREIGN KEY REFERENCES stringTable(id),
briefId INTEGER NOT NULL CONSTRAINT FOREIGN KEY REFERENCES stringTable(id)
);
The corresponding EF entity model would be something like this (entity type and property names are arbitrary):
[Table("stringTable")]
public class StringTable
{
[Column("id"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
// Navigation properties
public virtual ICollection<StringEntry> Entries { get; set; }
}
[Table("string")]
public class StringEntry
{
[Column("id"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Column("locale"), Required]
public string Locale { get; set; }
[Column("text"), Required]
public string Text { get; set; }
// Navigation properties
public virtual StringTable Table { get; set; }
}
[Table("elem")]
public class Element
{
[Column("id"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Column("name"), Required]
public string Name { get; set; }
[Column("titleId")]
public int TitleId { get; set; }
[Column("briefId")]
public int BriefId { get; set; }
// Navigation properties
public virtual StringTable Title { get; set; }
public virtual StringTable Brief { get; set; }
}
with composite PK and relationships mappings:
modelBuilder.Entity<StringEntry>()
.HasKey(e => new { e.Id, e.Locale });
modelBuilder.Entity<StringEntry>()
.HasRequired(e => e.Table)
.WithMany(e => e.Entries)
.HasForeignKey(e => e.Id)
.WillCascadeOnDelete();
modelBuilder.Entity<Element>()
.HasRequired(e => e.Title)
.WithMany()
.HasForeignKey(e => e.TitleId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Element>()
.HasRequired(e => e.Brief)
.WithMany()
.HasForeignKey(e => e.BriefId)
.WillCascadeOnDelete(false);
With all that being in place, you can access the associated strings with Element elem
as follows:
elem.Title.Entries
elem.Brief.Entries
project/extract the associated texts as follows:
TitleTexts = elem.Title.Entries.Select(e => e.Text)
BriefTexts = elem.Brief.Entries.Select(e => e.Text)
project/extract the text for a specific string locale
:
TitleText = elem.Title.Entries.Where(e => e.Locale == locale).Select(e => e.Text).FirstOrDefault()
BriefText = elem.Brief.Entries.Where(e => e.Locale == locale).Select(e => e.Text).FirstOrDefault()
etc.
Update: For EF Core, the entity model/data annotations needed are exactly the same as above, just the fluent configuration must use the EF Core equivalents (all these go to OnModelCreating
method override of your DbContext
derived class):
modelBuilder.Entity<StringEntry>()
.HasKey(e => new { e.Id, e.Locale });
modelBuilder.Entity<StringEntry>()
.HasOne(e => e.Table)
.WithMany(e => e.Entries)
.HasForeignKey(e => e.Id)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Element>()
.HasOne(e => e.Title)
.WithMany()
.HasForeignKey(e => e.TitleId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Element>()
.HasOne(e => e.Brief)
.WithMany()
.HasForeignKey(e => e.BriefId)
.OnDelete(DeleteBehavior.Restrict);