RobertoCarlos Melgar

RobertoCarlos Melgar

  • 1.5k
  • 159
  • 9.3k

modify a field of ten that I have in the table with entity f

May 27 2020 10:03 AM
Very good day. I have a table to which I only want to modify the image in a field
 
 
 
I just need to change the image field, for this I do the following
  1. public partial class tblProducto {  
  2.  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  3.  public tblProducto() {  
  4.   this.tblDetalleVentas = new HashSet < tblDetalleVenta > ();  
  5.   this.tblKardexes = new HashSet < tblKardex > ();  
  6.   this.tblDetalleIngresoProductos = new HashSet < tblDetalleIngresoProducto > ();  
  7.  }  
  8.   
  9.  public int Id {  
  10.   get;  
  11.   set;  
  12.  }  
  13.  public int Categoria_Id {  
  14.   get;  
  15.   set;  
  16.  }  
  17.  public int UnidadMedida_Id {  
  18.   get;  
  19.   set;  
  20.  }  
  21.  public string Codigo {  
  22.   get;  
  23.   set;  
  24.  }  
  25.  public string ProductoDescripcion {  
  26.   get;  
  27.   set;  
  28.  }  
  29.  public Nullable < decimal > PrecioVenta {  
  30.   get;  
  31.   set;  
  32.  }  
  33.  public Nullable < decimal > Stock {  
  34.   get;  
  35.   set;  
  36.  }  
  37.  public System.DateTime FechaCreado {  
  38.   get;  
  39.   set;  
  40.  }  
  41.  public string NombreImagen {  
  42.   get;  
  43.   set;  
  44.  }  
  45.  public byte[] Imagen {  
  46.   get;  
  47.   set;  
  48.  }  
  49.  public int Estado_Id {  
  50.   get;  
  51.   set;  
  52.  }  
  53.  public int Usuario_Id {  
  54.   get;  
  55.   set;  
  56.  }  
  57.   
  58.  public virtual tblCategoria tblCategoria {  
  59.   get;  
  60.   set;  
  61.  }  
  62.  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  63.  public virtual ICollection < tblDetalleVenta > tblDetalleVentas {  
  64.   get;  
  65.   set;  
  66.  }  
  67.  public virtual tblEstado tblEstado {  
  68.   get;  
  69.   set;  
  70.  }  
  71.  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  72.  public virtual ICollection < tblKardex > tblKardexes {  
  73.   get;  
  74.   set;  
  75.  }  
  76.  public virtual tblUnidade tblUnidade {  
  77.   get;  
  78.   set;  
  79.  }  
  80.  public virtual tblUsuario tblUsuario {  
  81.   get;  
  82.   set;  
  83.  }  
  84.  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  85.  public virtual ICollection < tblDetalleIngresoProducto > tblDetalleIngresoProductos {  
  86.   get;  
  87.   set;  
  88.  }  
  89. } 
I have the following method
  1. public static void ModificarImagen(tblProducto objProducto)    
  2. {    
  3.     using (GourmetEntities db = new GourmetEntities())    
  4.     {    
  5.         db.tblProductos.Attach(objProducto);    
  6.         db.Entry(objProducto).Property(x => x.Imagen).IsModified = true;    
  7.         db.SaveChanges();    
  8.     }    
  9. }  
in the button event I have the following code
  1. private void BtnModificar_Click(object sender, EventArgs e) {  
  2.  //try    
  3.  //{    
  4.  byte[] Imagen = null;  
  5.  Stream myStream = openFileDialog1.OpenFile();  
  6.  using(MemoryStream ms = new MemoryStream()) {  
  7.   myStream.CopyTo(ms);  
  8.   Imagen = ms.ToArray();  
  9.  }  
  10.  foreach(var item in ListaProductos) {  
  11.   
  12.   ObjProducto.Imagen = Imagen;  
  13.  }  
  14.  tblProducto.ModificarImagen(ObjProducto);  
  15.  //ObjProducto.Id = IdProducto;    
  16.  //ObjProducto. = "NO CAMBIAR";    
  17.  //ObjProducto.Estado_Id = 0;    
  18.  //ObjProducto.Imagen = Imagen;    
  19.   
  20.  //    MessageBox.Show("Datos modificados correctamente", "INFORMACIÓN DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);    
  21.  //}    
  22.  //catch (Exception ex)    
  23.  //{    
  24.   
  25.  //    MessageBox.Show("Ocurrio un problema, los datos no se modificaron", "INFORMACIÓN DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);    
  26.  //}    
  27. }
but I get the following error,
 
 
 
it tells me that all the fields are mandatory. How can I solve this problem if you please, since there are 1000 fields, I must write the thousand fields. please help with this problem for me
 
RM.

Answers (2)