Doubt about Many-to-Many EF Relationship

Asked

Viewed 83 times

6

I am doubtful in a following rule: I have N Products and N Coins. From this relationship I will have the table Productomoeda:

ProdutoMoeda
Id -  ProdutoId - MoedaID - Nome
1     2           1         ProdutoNome + MoedaNome
2     1           1         ProdutoNome + MoedaNome
3     2           2         ProdutoNome + MoedaNome

I also have a correspondent who markets N Productomoeda. However Correspondent also has N Stores that commercializes the N Productomoeda that is linked to the Correspondent. The store can only sell the products that were registered to the Correspondent, since the store depends on the Correspondent also.

Ex: Correspondent Walmart Trades - Kind Product and Card - in USD and BRL Currencies. Store 1 - which belongs to Correspondent Walmart can only register 1 or N products that Walmart markets.

Ficaria assim:

Would it be right to create a Pk for a table that was generated from an N-N and use as a relationship key in another table? I am trying to represent this in EF also but soon when I will relate Correspondent and Productomoeda using Productoccurforming already generates me some errors. I used the same example to map product and it worked, but to use her relationship with product:

ProdutoCorrespondente_ProdutoMoeda_Target_ProdutoCorrespondente_ProdutoMoeda_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

The map is like this:

public ProdutoCorrespondenteConfig()
    {

        HasKey(x => new { x.ProdutoCorrespondenteId, x.ProdutoMoedaId, x.CorrespondenteId });

        Property(x => x.ProdutoCorrespondenteId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(t => t.Nome)
           .IsRequired()
           .HasMaxLength(60);

        HasRequired(x => x.ProdutoMoeda)
            .WithMany(x => x.ProdutosCorrespondente)
            .HasForeignKey(x => x.ProdutoMoedaId);

        HasRequired(x => x.Correspondente)
            .WithMany(x => x.ProdutosCorrespondente)
            .HasForeignKey(x => x.CorrespondenteId);

    }
  • 1

    Allan, I found this link from a guy who has a question similar to yours, it doesn’t solve your problem. http://stackoverflow.com/questions/11754842/how-to-fix-the-number-of-properties-in-the-dependent-and-principal-roles-in-a-r

1 answer

6


It would be right to create a Pk for a table that was generated from an N-N and use as a relationship key in another table?

Yeah, but that’s not how you’re gonna get it:

HasKey(x => new { x.ProdutoCorrespondenteId, x.ProdutoMoedaId, x.CorrespondenteId });

The recommended modeling is like this:

public class ProdutoCorrespondente
{
    [Key]
    public int ProdutoCorrespondenteId { get; set; }
    [Index("IUQ_ProdutoCorrespondente_ProdutoMoedaId_CorrespondenteId", IsUnique = true, Order = 1)]
    public int ProdutoMoedaId { get; set; }
    [Index("IUQ_ProdutoCorrespondente_ProdutoMoedaId_CorrespondenteId", IsUnique = true, Order = 2)]
    public int CorrespondenteId { get; set; }

    [Required]
    [StringLength(60)]
    public String Nome { get; set; }

    public virtual ProdutoMoeda ProdutoMoeda { get; set; }
    public virtual Correspondente Correspondente { get; set; }
}
  • 1

    Oops, Vlw Gypsy. I just left Productspongeid, and he’s already created the index for the other fields. Hugs!

Browser other questions tagged

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