dapper-rainbow-mysql
dapper-rainbow-mysql copied to clipboard
InsertOrUpdate not return id
When I use the InsertOrUpdate function of the Rainbow, it returns 1 when it inserts and 2 when it updates. How can I get the id of the object that has been updated or inserted?
I have the version of dapper 1.50.2 and of Rainbow.MySql 0.10.1
Code:
public static Tuple<int, int> AddOrUpdateSesionCarrera(sesiones sesion, out Pk15Error pk15error)
{
pk15error = new Pk15Error();
try
{
using (var transaction = new TransactionScope())
{
// Operaciones sobre la base de datos.
Db newdb = IVS_OpenConnection.db;
OpenConnectionInstance connect = null;
if (!string.IsNullOrEmpty(IVS_OpenConnection.CadenaConexion))
{
connect = new OpenConnectionInstance(IVS_OpenConnection.CadenaConexion);
newdb = connect.db;
}
if (sesion.carrera != null)
{
var sesionId = newdb.Sesiones.InsertOrUpdate(sesion); //TODO InsertOrUpdate not return id.
sesion.carrera.idSesion = (int)sesionId;
var carreraId = newdb.Carrera.InsertOrUpdate(sesion.carrera);
connect?.Dispose();
/* Devolvemos el id de la sesion */
return new Tuple<int, int>((int)sesionId, (int)carreraId);
}
// Aceptamos la transacción.
transaction.Complete();
}
}
catch (OpenConexionDbException e)
{
pk15error.exception = e;
pk15error.errorType = EnumeradosIVS.TDapperError.DatabaseConnection;
return null;
}
catch (Exception e)
{
pk15error.exception = e;
pk15error.errorType = EnumeradosIVS.TDapperError.DatabaseQuery;
error.EscribeErrorBD("Error AddSesionCarrera: " + e.Message);
throw new Exception("Error AddSesionCarrera: " + e.Message);
}
return null;
}
Thank you @antonheryanto
there is 3 API for InsertOrUpdate
InsertOrUpdate(Id, new { Name = "test" }); //return Id (means param id first 0 than assigned the autoincrement value
InsertOrUpdate(new {UserId}, new { Name = "test" }); //using key other than id (UserId here)
InsertOrUpdate(new { Name = "test" }); //which will return db.Execute return (affected row)
so in your case in order to get sesion Id uses
var sesionId = newdb.Sesiones.InsertOrUpdate(sesion.Id, sesion);