Persist data in related tables (1 x N)

Asked

Viewed 101 times

5

I have these 2 related classes, and the requested class has a Collection of "Itenspedidos"...

The Request class:

public class PedidoDTO
    {
        public int pedidoID { get; set; }
        public int codigo { get; set; }
        public DateTime data { get; set; }

        public int clienteID { get; set; }
        public virtual ClienteDTO Cliente { get; set; }

        public virtual IEnumerable<ItemPedidoDTO> itensPedidos { get; set; }
    }

And the Itempedido class:

 public class ItemPedidoDTO
    {
        public int itemPedidoID { get; set; }
        public int quantidade { get; set; }
        public decimal preco { get; set; }
        public int porcentagemDesconto { get; set; }

        public int pedidoID { get; set; }
        public virtual PedidoDTO pedido { get; set; }

        public int produdoID { get; set; }
        public virtual ProdutoDTO produto { get; set; }
    }

My question is this:: How do I persist the data so that the Itempedido entity automatically receives the foreign key?


In the project I use: Code first, EF6, Migrations, generic repositories, implementation and mapping repositories with Fluent Api.

1 answer

1

Márcio, I work with Entity Framework Core, there is a certain difference but I believe it can help you solve your problem. However, consider using EF Core, especially if it is a new project. See more information for comparison of the two here.

First issue to consider is mapping (I didn’t mark all the fields):

public void Configure(EntityTypeBuilder<PedidoDTO> builder)
{
    builder.ToTable("Pedido");
    builder.HasKey(x => x.pedidoId);
    builder.Property(x => x.pedidoId).IsRequired().ValueGeneratedOnAdd();
    builder.HasMany(x => x.itensPedidos).WithOne(x => x.pedido).HasForeignKey(x => x.pedidoId).OnDelete(DeleteBehavior.Cascade);
}

In the mapping of the order item you should not reference the request, because the request has already made this link:

public void Configure(EntityTypeBuilder<ItemPedidoDTO> builder)
{
    builder.ToTable("ItemPedido");
    builder.HasKey(x => x.itemPedidoID);
    builder.Property(x => x.itemPedidoID).IsRequired().ValueGeneratedOnAdd();
    builder.Property(x => x.pedidoID).IsRequired();
}

Done mapping correctly, the insertion is simple, you can enter the request directly:

var pedido = new PedidoDTO()
{
    clienteId = 1,
    itensPedidos = new List<ItemPedidoDTO>
    {
       new ItemPedidoDTO()
       {
           produtoId = 10,
           quantidade = 5M,
       }),
    }
};

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

Well, note that the cases of cliente and of produto i only informed the id of each. If you add the entity the EF will try to insert it as well. For this not to happen you must make a Attach in the entity for the entity to recognize it. Or simply leave it as null that will work.

The answer follows that one orienteering. When I took a test in my scenario I came across the relationship problem I described above, fixed it worked normally.

I hope I’ve helped

  • I do not know if it is necessary to map the foreign entity (ItemPedidoDTO) to the dependent entity (PedidoDTO). It is sufficient that PedidoDTO have an aggregation ICollection<ItemPedidoDTO> virtual and public.

  • 1

    @Marceloshinitiuchimura really shouldn’t have to. But I prefer to map to be clear, mainly to inform the bank how will work the foreign key, in this example I put the "Ondelete Cascade".

Browser other questions tagged

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