简体   繁体   English

实体框架-代码优先-共享主键一对多关系

[英]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.

相关问题 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 实体框架-代码优先-没有共享主键时的实体拆分 - Entity Framework - Code First - Entity Splitting When There Is No Shared Primary Key Entity Framework 5.0代码首先是一对一和一对多的关系 - Entity Framework 5.0 code first one to one and one to many relationship 实体框架代码优先于隐含组合键的可选一对多关系 - Entity Framework Code First Optional One to Many Relationship on implied Composite Key 当一个实体具有复合主键时,Entity Framework Core 中的多对多关系 - Many-to-many relationship in Entity Framework Core when one entity has composite primary key 实体框架代码复合密钥的第一对一关系 - Entity Framework Code First One to One relationship on composite key 无需代码优先模型创建的一对多关系实体框架 - One To Many relationship entity framework without Code First Model Creation Code First Entity Framework不会创建一对多关系 - Code First Entity Framework doesn't create one to many relationship 实体框架6首先将多个表与一个外键关系代码 - Entity Framework 6 multiple table to one foreign key relationship code first EF 6非主键上与同一实体的代码优先多对多关系 - EF 6 Code-First Many to Many relationship with same entity on non-primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM