Add property in relation to N-N

Asked

Viewed 166 times

6

How to add properties (columns) in an N-N relation using EF?

For example, I have the class Produto:

public class Produto
{
    [Key]
    public int ProdutoID { get; set; }
    public string Descricao { get; set; }
    public decimal Valor { get; set; }

    public virtual ICollection<Venda> Vendas { get; set; }

    public Produto()
    {
        this.Vendas = new List<Venda>();
    }
}

And I got the class Venda:

public class Venda
{
    [Key]
    public int VendaID { get; set; }
    public string Descricao { get; set; }
    public decimal Total { get; set; }

    public virtual ICollection<Produto> Produtos { get; set; }

    public Venda()
    {
        this.Produtos = new List<Produto>();
    }
}

I’m using Code First and therefore EF automatically creates a table VendaProdutos, the problem is that this table should have more fields, as Quantidade, etc..

Is there any way I can do this without running away from Code First or will have to look for another way to map my classes (Fluent API for example)?

  • 1

    You will have to create a new class that will stay between your relationship in order to have a new property.

  • Then I would have to switch these N-N relations to 1-N relations right. I hadn’t thought about it.

  • Exactly, in case it would have a class ProdutoVenda in the middle.

  • 1

    Relations NxM are made in order to have an intermediate table that receives a relation 1xN of each of the other tables involved. In this specific case, the intermediate table (ProdutoVenda) must have a key composed by the key pair of the two external tables. And once this intermediary is to receive an entry, one must make sure that the quantity sold does not exceed the stock...

1 answer

7


I believe the only solution is to create a class for the relationship.

In your case, it would look like this:

Product class

public class Produto
{
    [Key]
    public int ProdutoID { get; set; }
    public string Descricao { get; set; }
    public decimal Valor { get; set; }

    public virtual ICollection<ProdutoVenda> ProdutosVenda { get; set; }

}

Sales class

    public class Venda
    {
        [Key]
        public int VendaID { get; set; }
        public string Descricao { get; set; }
        public decimal Total { get; set; }

        public virtual ICollection<ProdutoVenda> ProdutosVenda { get; set; }

    }

Classe Produtovenda

public class ProdutoVenda
{
    public int ProdutoId { get; set; }
    public int VendaID { get; set; }

    public virtual Produto Produto { get; set; }
    public virtual Venda Venda { get; set; }

    public int Quantidade { get; set; }
}
  • 1

    Those builders in Produto and Venda no longer needed. You can remove them from the answer. The other elements virtual the Entity Framework is responsible for.

  • @gypsy, thanks! I had forgotten this detail when I copied ;)

Browser other questions tagged

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