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 = "xxxxxx@yopmail.com",
                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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top