2

我有一堂课:

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

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

请使用正确的枚举值映射 accountstate 的任何解决方案?

4

1 回答 1

1

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

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

于 2013-07-03T10:28:47.087 回答