我有一个类:
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获取一个具有帐户的member。state=取消激活(值0)msss中执行的存储过程是正确的,并显示accountstate=1
有什么解决方案可以用枚举的正确值映射accountstate吗?
在我看来,这就像是一个映射问题。
C#中的默认枚举类型是int
,tinyint
映射到byte
的CLR
类型,因此您必须将enum
更改为byte
,或者相应地更改sql server数据类型。