I am developing an application where I need to keep an invoice, where the client and the detail of said invoice, here the code:
using(Context context = new Context())
{
Cliente cliente;
if (clienteActual != consumidorFinal)
{
cliente = new Cliente()
{
Nombre = txtClienteNombre.Text.Trim(),
Cedula = txtClienteCedula.Text.Trim(),
Status = Status.Activo
};
context.Cliente.Add(cliente);
}
else
{
cliente = ClienteActual;
}
context.Cliente.Attach(cliente);
cliente.CabeceraFactura = new List<CabeceraFactura>();
CabeceraFactura cabeceraFactura = new CabeceraFactura()
{
Fecha = DateTime.Today,
Status = Status.Activo,
Cliente = cliente
};
cabeceraFactura.DetalleFactura = new List<DetalleFactura>();
decimal total = 0;
foreach (DetalleFactura entry in detalleFacturaBindingSource)
{
total += (entry.PrecioUnitario * entry.Cantidad);
cabeceraFactura.DetalleFactura.Add(entry);
}
cabeceraFactura.Total = total;
cliente.CabeceraFactura.Add(cabeceraFactura);
context.SaveChanges();
return true;
}
The problem is that I jump
System.InvalidOperationException :
An entity object can not be referenced by multiple instances of IEntityChangeTracker.