2

I have a class:

   public class Member
{
    #region public property

    public int Id { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Mobile { get; set; }
    public string Fax { get; set; }
    public string OtherPhone { get; set; }
   ==> public AccountState AccountState { get; set; }
    public byte SiteId { get; set; }
    public int? GodFatherId { get; set; }

}

in this class , i have an enum public enum AccountState { NotActivated = 0, Activated = 1, Desactived = 2, BlackListed = 3, ActivatedWithoutMailInvitation = 5 }

To acces to database: i'm using this class:

  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;
    }
}

My repository call the dbcontext like this:

public Member GetMemberId(int memberId, IDbTransaction transacion = null)
        {
            var memberResult = _dbContext.ExecuteProcedure<Member>(MemberProcedures.P_Select, new { MemberId = memberId }).SingleOrDefault();
            return memberResult;
        }
        public void SaveMember(Member memberToSave, IDbTransaction transacion = null)
        {
            _dbContext.ExecuteProcedure(MemberProcedures.P_AddMember,
                new
                {
                    LastName = memberToSave.LastName,
                    FirstName = memberToSave.FirstName,
                    InitialEmail = memberToSave.Email,
                    Password=memberToSave.Password,
                    Email=memberToSave.Email,
                    Mobile=memberToSave.Mobile,
                    Fax=memberToSave.Fax,
                    OtherPhone=memberToSave.OtherPhone,
                    AccountStateId = (int)memberToSave.AccountState,
                    BirthDate = memberToSave.BirthDate,
                    Civility = memberToSave.Civility
                });
        }

in my test class,i add a member to the database

  Member memberToInsert = new Member()
            {
                Civility = "Monsieur",
                Email = "[email protected]",
                Password = "password",
                FirstName = "xxxx",
                LastName = "xxx",
                Site = new Site() { Id = 1 },
                BirthDate = new DateTime(1988,02,02),
                AccountState = AccountState.Activated,
                GodFatherId = 1
            };

            _memberRepository.SaveMember(memberToInsert);

the member is saved into the database with accountstate=1

but when i execute:

Member memberGetted = _memberRepository.GetMemberId(1824);

I Get a memeber with a account.state=desactivated (value 0) The stored procedure exexuted in msss is correct and show accountstate=1

Any solution please to map accountstate with the correct value of the enum?

5
  • What data type is AccountState in your database? Commented Jul 3, 2013 at 10:22
  • I have a AccountStateId and type is tinyint Commented Jul 3, 2013 at 10:25
  • Are you swalling any DataExceptions? Mapping a tinyint to a int (your enum) should cause one. Commented Jul 3, 2013 at 10:42
  • 1
    Just to be explicit - you can do this simply by adding : byte onto the enum declaration: public enum AccountState : byte { ... } Commented Jul 3, 2013 at 12:08
  • 1
    it's true i can use : byte but in the case of smallint=int16 i can't do public enum AccountState : int16 { ... } Commented Jul 3, 2013 at 13:35

1 Answer 1

1

It looks to me like a mapping issue.

The default enum type in C# is int, tinyint maps to the CLR type of byte so you'll either have to change your enum to byte or change the sql server data type accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.