[英]Entity Framework - Code first - One to Many relationship with shared primary key
Anyone know how to implement this scenario with EF Code First Fluent API: 任何人都知道如何使用EF Code First Fluent API来实现此方案:
public class Referancial
{
// Identity
public int KeyID { get; set; }
public string Code { get; set; }
public virtual ICollection<Translation> Translations { get; set; }
}
public class Translation
{
// refer to Referancial.KeyID
public int KeyID { get; set; }
public int LanguageID { get; set; }
public string Label { get; set; }
}
thank you for your response 感谢您的答复
Try with another model, I think that It will suit better for you 尝试其他型号,我认为它会更适合您
public class Referancial
{
// Identity
public int KeyID { get; set; }
public string Code { get; set; }
public virtual ICollection<Translation> Translations { get; set; }
}
public class Translation
{
//Translation needs its own Key
public int ID { get; set; }
// reference directly the Referencial object instead of the ID
public Referencial Referencial { get; set; }
public int LanguageID { get; set; }
public string Label { get; set; }
}
To Configure your model 配置模型
_modelBuilder.Entity<Referancial>().HasKey(r => r.KeyID);
_modelBuilder.Entity<Translation>().HasKey(t => t.ID);
_modelBuilder.Entity<Referancial>().HasMany(r => r.Translations).WithRequired(t => t.Referencial).WillCascadeOnDelete(true);
//On model creating : //关于模型创建:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { 受保护的重写void OnModelCreating(DbModelBuilder modelBuilder){
// Person
modelBuilder.Entity<Person>().HasKey(e => e.PersonID)
.ToTable("Persons")
.Property(e => e.PersonID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Referancial
modelBuilder.Entity<Referancial>().HasKey(e => e.KeyID)
.ToTable("Referancials")
.Property(e => e.KeyID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Translation
modelBuilder.Entity<Translation>().ToTable("Translations")
.HasKey(e => e.KeyID)
.Property(e => e.KeyID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Referancial>()
.HasOptional(e=>e.Translations)
.WithMany()
.HasForeignKey(e => e.KeyID);
base.OnModelCreating(modelBuilder);
}
but it shoz me an error : 但这使我犯了一个错误:
One or more validation errors were detected during model generation: 在模型生成期间检测到一个或多个验证错误:
PersonSearch.Models.Referancial_Translations: : Multiplicity conflicts with the referential constraint in Role 'Referancial_Translations_Target' in relationship 'Referancial_Translations'. PersonSearch.Models.Referancial_Translations::多重性与关系“ Referancial_Translations”中的角色“ Referancial_Translations_Target”中的引用约束冲突。 Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
因为从属角色中的所有属性都是不可为空的,所以主体角色的多重性必须为'1'。 Referancial_Translations_Source: : Multiplicity is not valid in Role 'Referancial_Translations_Source' in relationship 'Referancial_Translations'.
Referancial_Translations_Source ::多重性在关系“ Referancial_Translations”中的角色“ Referancial_Translations_Source”中无效。 Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.
由于从属角色指的是关键属性,因此从属角色的多重性上限必须为“ 1”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.