Saadi Makrem
Saadi Makrem

Reputation: 3

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:

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

Upvotes: 0

Views: 707

Answers (2)

Marc Cals
Marc Cals

Reputation: 2989

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);

Upvotes: 1

Saadi Makrem
Saadi Makrem

Reputation: 3

//On model creating :

protected override 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'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. Referancial_Translations_Source: : Multiplicity is not valid in Role 'Referancial_Translations_Source' in relationship 'Referancial_Translations'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

Upvotes: 0

Related Questions