Many-to-Many Relationship Entity Framework Code First Composite Primary Keys Repeat Column

Asked

Viewed 329 times

1

in my application I have two entities with a relationship of many to many, namely, Activity and Skill:

inserir a descrição da imagem aqui

The EF (Code First) generates for me the junction table, but it is giving conflict if I create the mapping in the hand Onmodelcreating because I have the same column (ID_CLIENTE) in both tables (and I cannot take ID_CLIENTE from either). If I let generate without mapping, creates the column ID_CLIENTE twice (with different names).

Is there any way to configure the table creation Skill_activity without me having to create the entity and relationships at hand?

Models:

public class Atividade
{
    public Atividade()
    {
        Skills = new List<Skill>();
    }

    public int Id { get; set; }
    public string CustomerId { get; set; }
    public virtual Cliente Cliente { get; set; }
    public virtual ICollection<Skill> Skills { get; set; }
}

public class Skill
{
    public Skill()
    {
        Activities = new List<Atividade>();
    }

    public int SkillId { get; set; }
    public string CustomerId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Score { get; set; }
    public virtual Cliente Customer { get; set; }
    public virtual ICollection<Atividade> Activities { get; set; }
}

API Code of the Fluent

            modelBuilder.Entity<Skill>()
            .HasMany(t => t.Activities)
            .WithMany(t => t.Skills)
            .Map(m =>
            {
                m.ToTable("SKILL_ATIVIDADE");
                m.MapLeftKey("SkillId", "CustomerId");
                m.MapRightKey("Id", "CustomerId");
            });

This code does not work because I repeat the property in the two entities, if I change the name of the property should work, but then I will have the same column with different names...

  • 1

    You can [Edit] and add in the question how are the models?

  • You set them to Foreign key?

  • @jbueno did the editing...

  • @Filipeandradelopes You are making the maps with Fluent or automatic?

  • @jbueno tried it both ways. As my bank uses a different pattern for table names it cannot be automatic, but the automatic generates the column ID_CLIENTE twice, one for each entity. I’ll put the Fluent API code.

  • @Denis so...what I did was try to map using the Fluent API, but I didn’t explicitly do Fks.

  • @Filipeandradelopes a look here: https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113). aspx

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.