Unire il framework di entità
-
27-10-2019 - |
Domanda
C'è un modo per chiamare T-SQL di T-SQL Comando da .NET Entity Framework 4?
Soluzione
No, non esiste una tale funzionalità integrata: devi costruire la tua. Molto comune è ad esempio un approccio come:
public void SaveOrUpdate(MyEntity entity)
{
if (entity.Id == 0)
{
context.MyEntities.AddObject(entity);
}
else
{
context.MyEntities.Attach(entity);
context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
// You can call SaveChanges here or you can call it separately after multiple changes
}
Questo è un esempio per lavorare con l'entità indipendente che hanno Id
Auto generato nel database (IDENTITY
). L'ID predefinito per la nuova entità è sempre 0 perché il valore reale verrà assegnato durante il salvataggio delle modifiche.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow