Doubt about Many-to-Many Relationship EF

6

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

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 owns N Stores that sells the N ProductoMoeda that is linked to the Correspondent. The store can only sell the products that have been registered to the Correspondent, since the store depends on the Correspondent as well.

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

Would it be okay to create a Pk for a table that was generated from one N-N and use it as a relationship key on another table? I'm trying to represent this in EF as well but as soon as I'm linking Correspondent and ProductMode using MatchProducts I already generate some errors. I used the same example to map ProductMode and it worked, but to use her relationship with MatchProducer, I got the error:

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

    }
    
asked by anonymous 17.05.2017 / 17:47

1 answer

6
  

Would it be okay to create a Pk for a table that was generated from one N-N and use it as a relationship key on another table?

Yes, but that's not how you're going to get it:

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

The recommended modeling looks 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; }
}
    
17.05.2017 / 17:53