Relationship 1 - N Entity Framework

1

I'm developing a test project and I'm having a problem with 1-N relationship with Entity.

Model Cab_Venda:

  public Cab_Venda() {
   Det_Venda = new HashSet < Det_Venda > ();
  }

  public int id {
   get;
   set;
  }

  public int idCliente {
   get;
   set;
  }

  [Column(TypeName = "date")]
  public DateTime data {
   get;
   set;
  }

  [Required]
  [StringLength(6)]
  public string hora {
   get;
   set;
  }

  [Required]
  [StringLength(1)]
  public string situacao {
   get;
   set;
  }

  [Column(TypeName = "date")]
  public DateTime dataExpedicao {
   get;
   set;
  }

  [Column(TypeName = "date")]
  public DateTime dataAceite {
   get;
   set;
  }

  public virtual Usuario Usuario {
   get;
   set;
  }

  public virtual ICollection < Det_Venda > Det_Venda {
   get;
   set;
  }

Model Det_Venda

    public int id {
  get;
  set;
 }

 public int idCab {
  get;
  set;
 }

 public int idProduto {
  get;
  set;
 }

 public int idPreco {
  get;
  set;
 }

 public virtual Cab_Venda Cab_Venda {
  get;
  set;
 }

 public virtual Preco Preco {
  get;
  set;
 }

 public virtual Produtos Produtos {
  get;
  set;
 }

I'm receiving a Products collection and I need to enter a Cab_Venda and with the products of this sale in Det_Venda . In my Controller I have the following code:

    List < Det_Venda > listDetVenda = new List < Det_Venda > ();

using(var db = new LojaAppContext()) {

 var idUsuario = Int32.Parse(Session["idUsuario"].ToString());


 var carrinho = db.Carrinho.Where(p => p.idUsuario == idUsuario).ToList();

 Cab_Venda order = new Cab_Venda() {
  data = DateTime.Now,
   hora = "142626",
   situacao = "Aguardando liberação",
   dataExpedicao = DateTime.Now,
   dataAceite = DateTime.Now
 };

 db.Cab_Venda.Add(order);
 db.SaveChanges();


 List < Det_Venda > listProdutos = new List < Det_Venda > ();

 foreach(var item in carrinho) {

  Preco preco = db.Preco.FirstOrDefault(p => p.idProduto == item.idProduto);

  Det_Venda dt = new Det_Venda() {
   idCab = order.id,
    idProduto = item.idProduto,
    idPreco = preco.id
  };

  listProdutos.Add(dt);
 }

 listProdutos.ForEach(dt => db.Det_Venda.Add(dt));
 db.SaveChanges();
}

But when saving the following error:

  

Validation failed for one or more entities. See   'EntityValidationErrors' property for more details.

Linha 41:                 db.Cab_Venda.Add(order);
Linha 42:                 db.SaveChanges();

Could anyone tell me what it could be?

    
asked by anonymous 10.12.2017 / 20:33

1 answer

0

EF mapping, you get to work that way dynamically, you want 1-N of type 1 Client has N requests?

If yes,

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

    public virtual ICollection<Pedido> Pedidos {get; set;}
}

public class Pedido
{
    public int Id { get; set;}
    public int PedidoId {get; set;}
    public DateTime Data {get; set;}

    public virtual Cliente Cliente {get; set;}
}

EF works with conventions, so if you speak ENTITY + ID (ClientId, RequestId), but that's for another question. If you want to map in the hand, it is simple also in the mapping class of the request include;

public class PedidoMap : EntityTypeConfiguration<Pedido>
{
    public PedidoMap()
    {
        ToTable("Pedidos");

        HasRequired(x => x.Cliente)
            .WithMany(x => x.Pedidos)
            .HasForeignKey(x => x.PedidoId);
    }
}
    
11.12.2017 / 14:34