← Tutti gli articoli

Entity Data Framework - Delete a record through the primary key. EntityKey (namespace System.Data)

05 luglio 2010  · EDF · Article  ·  32 visite

Entity Data Framework - Delete a record through the primary key. EntityKey (namespace System.Data)

In the following block code I'll show how to delete a record in the table Tbl_SpidBustaPaga through the primary key (ID_SpidBustaPaga):

 {          
int ID_SpidBustaPaga = Convert.ToInt32(hid_ID_SpidBustaPaga.Value);
ContraEntities contraEntityCtx = new ContraEntities(ConfigurationManager.ConnectionStrings["ContraCSEntities"].ToString());
var BustaPagaKey = new EntityKey("ContraEntities.Tbl_SpidBustaPaga", "ID_SpidBustaPaga", ID_SpidBustaPaga);
var contact = contraEntityCtx.GetObjectByKey(BustaPagaKey);
contraEntityCtx.DeleteObject(contact);
contraEntityCtx.SaveChanges();
}