stored procedures - Dapper mapping column with a property in the model having a different name and type -
i have model model:
public class member { #region public property public int id { get; set; } public string lastname { get; set; } public string firstname { get; set; } public accountstate accountstate { get; set; } public godfathertype godfathertype { get; set; } }
accountstate , godfathertype both 2 eumerates:
public enum accountstate { notactivated = 0, activated = 1, desactived = 2, } public enum godfathertype { undefined=0, unknown = 1, correct = 2, }
in database have id, lastname, fistname, tinyint
accountstateid et smallint
godfathertypeid, don't change stored procedure how can map class member
database??
actually attributes id, lastname, fistname when execute stored procedure code:
public sealed class dbcontext : idbcontext { private bool disposed; private sqlconnection connection; public dbcontext(string connectionstring) { connection = new sqlconnection(connectionstring); } public idbconnection connection { { if (disposed) throw new objectdisposedexception(gettype().name); return connection; } } public idbtransaction createopenedtransaction() { if (connection.state != connectionstate.open) connection.open(); return connection.begintransaction(); } public ienumerable<t> executeprocedure<t>(string procedure, dynamic param = null, idbtransaction transaction = null) { if (connection.state == connectionstate.closed) { connection.open(); } return dapper.sqlmapper.query<t>(connection, procedure, param, transaction, commandtype: commandtype.storedprocedure); } public int executeprocedure(string procedure, dynamic param = null, idbtransaction transaction = null) { if (connection.state == connectionstate.closed) { connection.open(); } return dapper.sqlmapper.execute(connection, procedure, param, transaction, commandtype: commandtype.storedprocedure); } public void dispose() { debug.writeline("** disposing dbcontext"); if (disposed) return; if (connection != null) { connection.dispose(); connection = null; } disposed = true; } }
the easiest option keep them 1:1, i.e.
public accountstate accountstateid { get; set; } public godfathertype godfathertypeid { get; set; }
where
public enum accountstate : byte {...} public enum godfathertype : short {...}
if renaming properties isn't possible, maybe add shim properties:
private byte accountstateid { { return (byte)(int)accountstate; } set { return accountstate = (accountstate)(int)value; } }
it is possible rename members in map, more complicated.
Comments
Post a Comment