Best way to make class relationship

Asked

Viewed 849 times

4

Hello, I would like to know what is the correct way to make relationship between classes of 1.. N and 1...1. Example:

public class Pedido
{
    public int PedidoID{get;set;}
    .............
}

public class ItemPedido
{
    public int ItemPedidoID{get;set;}
    public int PedidoId{get;set;}
    public int ProdutoId{get;set;}
    .........
}

public class Produto
{
    public int ProdutoId{get;set;}
    ...........
}
  • It depends, right. Are you using any ORM? If so, which one? Or do you not even want to connect to the database?

2 answers

6

I would model the classes as follows:

public class Pedido
{
    public int Id { get; set; }
    public DateTime Data { get; set; }
    public virtual List<Produto> Produtos { get; set; }
}

public class Produto
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

public class Context : DbContext
{
    public Context() : base("DefaultConnection")
    {
    }
    public DbSet<Pedido> Pedidos { get; set; }
    public DbSet<Produto> Produtos { get; set; }
}

In this case I used the virtual for the example, if I was using EF as ORM, I also changed Pedidoid and Produtoid to Id, just to stay standard.

Remembering that in this example are showing Objects of our application, and not in the Data Model (SQL).

If your "Itempedido" in the database has no property (other than the Product Id and Order Id), it is not necessary to create an intermediate object in C# called Itempedido, it makes sense to create this object if your Itempedido has Quantity for example.

Below is an example of how to use a console:

class Program
{
    static void Main(string[] args)
    {
        var context = new Context();

        var produto = new Produto() { Nome = "Produto 1" };
        var produto2 = new Produto() { Nome = "Produto 2" };

        context.Produtos.Add(produto);
        context.Produtos.Add(produto2);
        context.SaveChanges();

        var pedido = new Pedido() { Data = DateTime.Now };
        pedido.Produtos = new List<Produto>();
        pedido.Produtos.Add(produto);
        pedido.Produtos.Add(produto2);

        context.Pedidos.Add(pedido);
        context.SaveChanges();


    }
}
  • 1

    Why Product Do you have a wish list? If the answer is because the same product can appear in more than one order, the correct modeling would be to have a unique class to represent the product on request, which is different from the class of the product itself. Moreover, why two Savechanges? If you leave only the last Savechanges the result is the same, and performing all bank manipulations of a given operation at once is the most common practice.

  • The two Savechanges were just an example, and yes, you’re right, you don’t need the Product Order collection (you only need it if you want to retrieve orders for products). I edited my answer again :)

2

The beginning is correct. Navigation properties need to be defined for use by the Entity Framework, for example:

public class Pedido
{
    public int PedidoId {get;set;}

    public virtual ICollection<ItemPedido> ItemPedidos { get; set; }
}

public class ItemPedido
{
    public int ItemPedidoId {get;set;}
    public int PedidoId {get;set;}
    public int ProdutoId {get;set;}

    // Acho que aqui deveria vir uma quantidade, certo?
    public int Quantidade { get; set; }

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

public class Produto
{
    public int ProdutoId {get;set;}

    public virtual ICollection<ItemPedido> ItemPedidos { get; set; }
}

Here there are no cases 1.. 0-1. Only 1.. N and N.. 1, so I will invent a case 1.. 0-1.

Suppose your Pedido have a PedidoEnderecoEntrega. It may exist or not (i.e., 0 or 1 record). The modeling would look like this:

public class PedidoEnderecoEntrega
{
    [Key, ForeignKey("Pedido")]
    public int PedidoId { get; set; } // É PedidoId mesmo. A chave primária também é estrangeira.

    ...
    public virtual Pedido Pedido { get; set; }
}

Pedido would look like this:

public class Pedido
{
    public int PedidoId {get; set;}

    public virtual ICollection<ItemPedido> ItemPedidos { get; set; }
    public virtual PedidoEnderecoEntrega PedidoEnderecoEntrega { get; set; }
}
  • Thank you that your reply helped me a lot.

  • @Alyssonbormann Could you please tell me if you are using the Entity Framework? Can I still improve this answer.

  • I’m using Entity yes

Browser other questions tagged

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