public void UpsertEntity <Tentity>(int?identityValue, ref Tentity entityObject, AsyncAutomateAccountingEntities dbContext) where Tentity : class { if (identityValue.HasValue) { dbContext.Entry(entityObject).State = System.Data.Entity.EntityState.Modified; } else { dbContext.Set <Tentity>().Add(entityObject); } dbContext.SaveChanges(); }