Stored procedures 模型中属性具有不同名称和类型的Dapper映射列
我有一个模型这个模型:Stored procedures 模型中属性具有不同名称和类型的Dapper映射列,stored-procedures,sql-server-2008-r2,mapping,dapper,sqlclient,Stored Procedures,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 AccountState AccountState { get; set
public class Member
{
#region public property
public int Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public AccountState AccountState { get; set; }
public GodFatherType GodFatherType { get; set; }
}
AccountState和GodFatherType都是2个EU速率:
public enum AccountState
{
NotActivated = 0,
Activated = 1,
Desactived = 2,
}
public enum GodFatherType
{
Undefined=0,
unknown = 1,
Correct = 2,
}
在数据库中,我有Id、LastName、FistName、aTinyInt
AccountstateId etsmallint
GodFatherTypeid,我不想更改我的存储过程如何将我的类成员
映射到数据库
实际上,当我使用以下代码执行存储过程时,我只获得属性Id、LastName和FistName:
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:IDbContext
{
私人住宅;
专用SqlConnection;
公共数据库上下文(字符串连接字符串)
{
连接=新的SqlConnection(connectionString);
}
公共连接
{
得到
{
如果(disposed)抛出新的ObjectDisposedException(GetType().Name);
回路连接;
}
}
公共IDbTransaction CreateOpenedTransaction()
{
if(connection.State!=ConnectionState.Open)
Connection.Open();
返回连接。BeginTransaction();
}
公共IEnumerable ExecuteProcess(字符串过程,动态参数=null,IDbTransaction=null)
{
if(connection.State==ConnectionState.Closed)
{
connection.Open();
}
返回Dapper.SqlMapper.Query(连接、过程、参数、事务、,
commandType:commandType.StoredProcess);
}
public int ExecuteProcedure(字符串过程,动态参数=null,IDbTransaction=null)
{
if(connection.State==ConnectionState.Closed)
{
connection.Open();
}
返回Dapper.SqlMapper.Execute(连接、过程、参数、事务、,
commandType:commandType.StoredProcess);
}
公共空间处置()
{
Debug.WriteLine(“**DbContext”);
如果(处置)返回;
if(连接!=null)
{
connection.Dispose();
连接=空;
}
这是真的;
}
}
最简单的选择是将它们保持为1:1,即
public AccountState AccountStateId { get; set; }
public GodFatherType GodFatherTypeId { get; set; }
在哪里
如果无法重命名属性,可以添加垫片属性:
private byte AccountStateId {
get { return (byte)(int)AccountState; }
set { return AccountState = (AccountState)(int)value; }
}
也可以重命名映射中的成员,但这更复杂。存储过程会返回哪些列?到目前为止,它看起来像是一个直接匹配-它是否像修复枚举一样简单?i、 e.
public enum AccountState:byte{NotActivated=…}
和public enum godfat类型:short{…}
?这些是我能看到的唯一的不匹配…我对第一个解决方案没问题,谢谢:)
private byte AccountStateId {
get { return (byte)(int)AccountState; }
set { return AccountState = (AccountState)(int)value; }
}