The entity type Cargo is not part of the model for the curre

Apr 27 2020 11:53 AM
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
  1. namespace CapaDatos  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class tblCargo  
  7.     {  
  8.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  9.         public tblCargo()  
  10.         {  
  11.             this.tblEmpleados = new HashSet<tblEmpleado>();  
  12.         }  
  13.       
  14.         public int Id { getset; }  
  15.         public string Nombre_Cargo { getset; }  
  16.       
  17.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  18.         public virtual ICollection<tblEmpleado> tblEmpleados { getset; }  
  19.     }  
  20. }  
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
  1. namespace CapaDatos  
  2. {  
  3.     public partial class tblCargo  
  4.     {  
  5.         public static void Modificar(tblCargo tblCargo)  
  6.         {  
  7.             using (GourmetEntities db = new GourmetEntities())  
  8.             {  
  9.                 try  
  10.                 {  
  11.                     db.Entry(tblCargo).State = EntityState.Modified;  
  12.                     db.SaveChanges();  
  13.                 }  
  14.                 catch (Exception)  
  15.                 {  
  16.   
  17.                     throw;  
  18.                 }  
  19.             }  
  20.         }  
  21.     }  
  22. }  
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
  1. private void TsbModificar_Click(object sender, EventArgs e)  
  2.         {  
  3.             objCargo.Id = int.Parse(TxtId.Text);  
  4.             objCargo.Nombre_Cargo = TxtCargo.Text;  
  5.             Cargo.Modificar(objCargo);  
  6.             MessageBox.Show("Registro con exito");  
  7.   
  8.         }  
when i run the application it shows me the following error
 
 
 
Please can you help me find my mistake,
Thanks a lot
Ro

Answers (1)