Nel caso che c'è la necessità di duplicare un record, può capitare che al secondo salvataggio, generi un errore.
Il frammento di codice qui di seguito, illustra una tecnica di come inserire un record uguale ad uno precedentemente salvato, ma con una chiave diversa.
if (EntitaNew.EntityKey == null || EntitaNew.EntityKey.IsTemporary)
{
Oggetto.GetDataContext().TABELLA.AddObject(EntitaNew);
}
else
{
Oggetto.GetDataContext().TABELLA.Attach(EntitaNew);
}
Oggetto.GetDataContext().ObjectStateManager.ChangeObjectState(EntitaNew, EntityState.Added);
Oggetto.SaveChanges();
|
Programmazione .Net, Asp.Net, Compact Framework, MAUI, Sql Server, Filenet e tanto altro materiale informatico.
Nessun commento:
Posta un commento