【发布时间】:2022-01-19 16:21:12
【问题描述】:
我需要 EF6 中的结构,其中项目颜色可以有多个翻译。这种结构是否支持该功能,是否正确?
我正在尝试应用以下关系:
- 每个 PIMItem 有 1 个 PIMColor
- 每个 PIMColor 可以有多个 PIMColorTranslations
- 每个语言代码的每种颜色只允许 1 个 PIMColorTranslation。
[Table("PIMItem")]
public class PIMItem
{
[Key]
public string Id {get;set;}//RowKey no.
//there are additional columns in this item not related to color
[ForeignKey("PIMColor")]
[Column(Order=3)]
public string ColorId {get;set;}
public PIMColor PIMColor {get;set;}
}
[Table("PIMColor")]
public class PIMColor
{
[Key]
public string ColorId {get;set;}
public ICollection<PIMItem> PIMItems {get;set;}
public ICollection<PIMColorTranslation> PIMColorTranslation {get;set;}
}
[Table("PIMColorTranslation")]
public class PIMColorTranslation{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order=1)]
public int Id {get;set;}
[ForeignKey("PIMLanguage")]
[Column(Order=2)]
public string LanguageCode {get;set;}
[ForeignKey("PIMColor")]
[Column(Order=3)]
public string ColorId {get;set;}
public string Translation {get;set;}
}
[Table("PIMLanguage")]
public class PIMLanguage{
[Key]
public string LanguageCode {get;set;}
public string Language {get;set;}
public ICollection<PIMColorTranslation> PIMColorTranslation {get;set;}
}
public class SharedCtxt : DbContext
{
public SharedCtxt() : base(sharedData.conn_string)
{
}
public DbSet<PIMLanguage> PIMLanguages {get;set;}
public DbSet<PIMColor> PIMColors {get;set;}
public DbSet<PIMColorTranslation> PIMColorTranslations {get;set;}
public DbSet<PIMItem> PIMItems {get;set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
【问题讨论】:
标签: c# entity-framework-6