将dapper与enum + sqlserver映射时出错

时间:2013-07-03 10:11:25

标签: c#-4.0 sql-server-2008-r2 mapping dapper sqlclient

我有一个班级:

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

}

在这堂课上,我有一个枚举 public enum AccountState { NotActivated = 0, Activated = 1, Desactived = 2, BlackListed = 3, ActivatedWithoutMailInvitation = 5 }

访问数据库:我正在使用这个类:

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

我的存储库调用dbcontext,如下所示:

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

在我的测试类中,我将一个成员添加到数据库

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

使用accountstate=1

将成员保存到数据库中

但是当我执行时:

Member memberGetted = _memberRepository.GetMemberId(1824);

我使用account.state = desactivated(值0)获取一个memeber 在msss中执行的存储过程是正确的,并显示accountstate=1

任何解决方案请将帐户状态映射到枚举的正确值?

1 个答案:

答案 0 :(得分:0)

在我看来,这是一个映射问题。

C#中的默认枚举类型为inttinyint映射到CLR类型的byte,因此您必须更改enumbyte或相应地更改sql server数据类型。