RobertoCarlos Melgar

RobertoCarlos Melgar

  • 1.5k
  • 159
  • 9.3k

load master detail from entity framework

Apr 5 2020 2:57 PM
Good afternoon, I need to show master detail data, I already have the data in the database. how do you see next
I also have the classes in the model
  1. namespace CapaDatos  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class tblMaestroVenta  
  7.     {  
  8.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  9.         public tblMaestroVenta()  
  10.         {  
  11.             this.tblDetalleVentas = new HashSet();  
  12.         }  
  13.       
  14.         public int Id { getset; }  
  15.         public string NumeroVenta { getset; }  
  16.         public System.DateTime FechaVenta { getset; }  
  17.         public Nullable<int> IdCliente { getset; }  
  18.         public double ValorVenta { getset; }  
  19.         public int IdUsuario { getset; }  
  20.       
  21.         public virtual tblCliente tblCliente { getset; }  
  22.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  23.         public virtual ICollection tblDetalleVentas { getset; }  
  24.     }  
  25. }  

a MasterSale class that inherits from tblMaestroventa
  1. namespace CapaLogica  
  2. {  
  3.     public class MaestroVenta : tblMaestroVenta  
  4.     {  
  5.         public string NombreCliente { getset; }  
  6.         public string NombreUsuario { getset; }  
  7.         // Guardar  
  8.         public static int GuardarMaestroVenta(MaestroVenta maestroVenta)  
  9.         {  
  10.             using (GourmetEntities db = new GourmetEntities())  
  11.             {  
  12.                 try  
  13.                 {  
  14.                     var returnCode = new SqlParameter("@ReturnValueId", SqlDbType.Int);  
  15.                     returnCode.Direction = ParameterDirection.Output;  
  16.                     db.Database.ExecuteSqlCommand("Sp_InsertarMaestroVenta @FechaVenta,@IdCliente,@ValorVenta,@IdUsuario, @ReturnValueId OUTPUT",  
  17.                 new SqlParameter("@FechaVenta", maestroVenta.FechaVenta),  
  18.                 new SqlParameter("@IdCliente", maestroVenta.IdCliente),  
  19.                 new SqlParameter("@ValorVenta", maestroVenta.ValorVenta),  
  20.                 new SqlParameter("@IdUsuario", maestroVenta.IdUsuario),  
  21.                 returnCode);  
  22.                     db.SaveChanges();  
  23.                     return (int)returnCode.Value;  
  24.                 }  
  25.                 catch (Exception exp)  
  26.                 {  
  27.                     var me = exp.Message;  
  28.                     throw;  
  29.                 }  
  30.                   
  31.                  
  32.             }  
  33.         }  
  34.         public static tblMaestroVenta MaestroDetalle(int Id)  
  35.         {  
  36.             using (GourmetEntities db = new GourmetEntities())  
  37.             {  
  38.                 return db.tblMaestroVentas.Include(x => x.tblDetalleVentas)  
  39.                     .FirstOrDefault(x => x.Id == Id);  
  40.             }  
  41.         }  
  42.   
  43.     }  
  44. }  
I also have the detail class
  1. namespace CapaDatos  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class tblDetalleVenta  
  7.     {  
  8.         public int Id { getset; }  
  9.         public int IdtblMaestroVenta { getset; }  
  10.         public int IdProducto { getset; }  
  11.         public int IdUnidadMedida { getset; }  
  12.         public int IdCategoria { getset; }  
  13.         public decimal Precio { getset; }  
  14.         public int Cantidad { getset; }  
  15.         public decimal ValorFila { getset; }  
  16.       
  17.         public virtual tblCategoria tblCategoria { getset; }  
  18.         public virtual tblMaestroVenta tblMaestroVenta { getset; }  
  19.         public virtual tblProducto tblProducto { getset; }  
  20.         public virtual tblUnidad tblUnidad { getset; }  
  21.     }  
  22. }  
A SalesDetail class inherited from tblDetalleVenta
  1. namespace CapaLogica  
  2. {  
  3.     public class DetalleVenta : tblDetalleVenta  
  4.     {  
  5.         //public int IdProducto { get; set; } // 0 Se guarda en la BD  
  6.         public string Codigo { getset; } // 0  
  7.         public string Descripcion { getset; } // 1  
  8.                                                 // public decimal PrecioVenta { get; set; } // 2  
  9.         public string Unidad { getset; } // 3  
  10.         public decimal TotalFila { get { return Precio * (decimal)Cantidad; } } // 5  
  11.         public static void InsertarDetalleVenta(tblDetalleVenta detalleVenta)  
  12.         {  
  13.             using (GourmetEntities db = new GourmetEntities())  
  14.             {  
  15.                 db.Sp_InsertarDetalleVenta(detalleVenta.IdtblMaestroVenta, detalleVenta.IdProducto, detalleVenta.IdUnidadMedida, detalleVenta.IdCategoria, detalleVenta.Precio, detalleVenta.Cantidad, detalleVenta.ValorFila);  
  16.             }  
  17.         }  
  18.   
  19.           
  20.     }  
  21. }  
I have a method that returns the query
  1. public static tblMaestroVenta MaestroDetalle(int Id)  
  2. {  
  3.     using (GourmetEntities db = new GourmetEntities())  
  4.     {  
  5.         return db.tblMaestroVentas.Include(x => x.tblDetalleVentas)  
  6.             .FirstOrDefault(x => x.Id == Id);  
  7.     }  
  8. }  
I use all this with entity Framework
now I have my RDLC Report
 
to load in the form I made this code
  1. private void FrmRptVenta_Load(object sender, EventArgs e)  
  2. {  
  3.     MaestroVentaBindingSource.DataSource = MaestroVenta.MaestroDetalle(1);  
  4.     DetalleVentaBindingSource.DataSource = MaestroVenta.MaestroDetalle(1);
  5.     this.reportViewer1.RefreshReport();
  6. }  
but only load part of master as you can see in the following image


Please can someone help me to solve this problem, or can you give me an example of how to solve this problem please. I appreciate the collaboration
Robert

Answers (3)