Dapper mapping column with a property in the model having a different name and type

dapper mapping sqlclient sql-server-2008-r2 stored-procedures

Question

I have a model this 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 and GodFatherType Are both 2 eumerates:

 public enum AccountState 
{
    NotActivated = 0,
    Activated = 1,
    Desactived = 2,

}

 public enum GodFatherType
    {
        Undefined=0,
        unknown = 1,
        Correct = 2,
    }

In The database I have Id, LastName, FistName, a TinyInt AccountstateId et smallint GodFatherTypeid, i don't would like to change my stored procedure how can I map My class Member with the database??

Actually I get only the attributes only Id, LastName, FistName when I execute my stored procedure with this code:

 public sealed class DbContext : IDbContext
{
    private bool disposed;
    private SqlConnection connection;

    public DbContext(string connectionString)
    {
        connection = new SqlConnection(connectionString);
    }

    public IDbConnection Connection
    {
        get
        {
            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;
    }
}

Accepted Answer

The easiest option is to 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 the properties isn't possible, maybe add shim properties:

private byte AccountStateId {
    get { return (byte)(int)AccountState; }
    set { return AccountState = (AccountState)(int)value; }
}

It is also possible to rename members in the map, but that is more complicated.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why