Hello friends good morning, I have a problem with the Entity Framework more properly.
I have the following entity that comes from the db with entity framework
- namespace CapaDatos
- {
- using System;
- using System.Collections.Generic;
-
- public partial class tblCargo
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public tblCargo()
- {
- this.tblEmpleados = new HashSet<tblEmpleado>();
- }
-
- public int Id { get; set; }
- public string Nombre_Cargo { get; set; }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection<tblEmpleado> tblEmpleados { get; set; }
- }
- }
As you will see the class is partial
this is why I create another file on the same layer called OperationsCargos
Rename the class to tblCargo
- namespace CapaDatos
- {
- public partial class tblCargo
- {
- public static void Modificar(tblCargo tblCargo)
- {
- using (GourmetEntities db = new GourmetEntities())
- {
- try
- {
- db.Entry(tblCargo).State = EntityState.Modified;
- db.SaveChanges();
- }
- catch (Exception)
- {
-
- throw;
- }
- }
- }
- }
- }
this to have my methods in the same layer but in another file, that if I delete the entity my methods are lost
this is the btn code
- private void TsbModificar_Click(object sender, EventArgs e)
- {
- objCargo.Id = int.Parse(TxtId.Text);
- objCargo.Nombre_Cargo = TxtCargo.Text;
- Cargo.Modificar(objCargo);
- MessageBox.Show("Registro con exito");
-
- }
when i run the application it shows me the following error
Please can you help me find my mistake,
Thanks a lot
Ro