在SQLite和Dapper中映射时间跨度
本文关键字:映射 时间跨度 Dapper SQLite | 更新日期: 2023-09-27 18:31:41
我正在尝试使用 Dapper 与现有数据库格式进行接口,该格式具有一个表,其持续时间在 BIGINT 列中编码为刻度。 如何告诉 Dapper 在插入和读取数据库时将 POCO 的 TimeSpan
类型属性映射到刻度?
我尝试将TimeSpan
的类型映射设置为DbType.Int64
:
SqlMapper.AddTypeMap(typeof(TimeSpan), DbType.Int64);
我也创建了一个ITypeHandler
,但从未调用SetValue
方法:
public class TimeSpanToTicksHandler : SqlMapper.TypeHandler<TimeSpan>
{
public override TimeSpan Parse(object value)
{
return new TimeSpan((long)value);
}
public override void SetValue(IDbDataParameter parameter, TimeSpan value)
{
parameter.Value = value.Ticks;
}
}
这是我的POCO:
public class Task
{
public TimeSpan Duration { get; set; }
// etc.
}
执行像这样的简单插入语句时:
string sql = "INSERT INTO Tasks (Duration) values (@Duration);";
并将 POCO 作为要插入的对象传递:
Task task = new Task { Duration = TimeSpan.FromSeconds(20) };
connection.Execute(sql, task);
我得到这个异常:
System.InvalidCastException : Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'.
at System.Convert.ToInt64(Object value, IFormatProvider provider)
at System.Data.SQLite.SQLiteStatement.BindParameter(Int32 index, SQLiteParameter param)
at System.Data.SQLite.SQLiteStatement.BindParameters()
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action`2 paramReader) in SqlMapper.cs: line 3310
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, ref CommandDefinition command) in SqlMapper.cs: line 1310
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in SqlMapper.cs: line 1185
如果我保持TimeSpan
类型映射不变(默认为 DbType.Time
),它会写入TimeSpan
的字符串版本,即 '00:00:20.000",这没有帮助,因为它与列中其他数据的格式不匹配。
你能做以下事情吗?
public class Task
{
public TimeSpan Duration { get; set; }
public long Ticks
{
get { return Duration.Ticks; }
set { Duration = new TimeSpan(value); }
}
// etc.
}
string sql = "INSERT INTO Tasks (Duration) values (@Ticks);";
LinqToDB 解决方案:
MappingSchema.SetDataType(typeof(TimeSpan), DataType.NText);
或:
MappingSchema.SetDataType(typeof(TimeSpan), DataType.Int64);
例:
public class Program
{
private const string ConnectionString = "Data Source=:memory:;Version=3;New=True;";
public static void Main()
{
var dataProvider = new SQLiteDataProvider();
var connection = dataProvider.CreateConnection(ConnectionString);
connection.Open();
var dataConnection = new DataConnection(dataProvider, connection);
dataConnection.MappingSchema.SetDataType(typeof(TimeSpan), DataType.Int64);
dataConnection.CreateTable<Category>();
dataConnection.GetTable<Category>()
.DataContextInfo
.DataContext
.Insert(new Category
{
Id = 2,
Time = new TimeSpan(10, 0, 0)
});
foreach (var category in dataConnection.GetTable<Category>())
{
Console.WriteLine($@"Id: {category.Id}, Time: {category.Time}");
}
}
private class Category
{
public int Id { get; set; }
public TimeSpan Time { get; set; }
}
}
我也想在 TimeSpan
和 DbType.Int64
之间进行转换,我还发现我的 ITypeHandler
实现上的 SetValue
方法从未被调用。
发现除了注册我的类型处理程序外,我还必须删除TimeSpan
和TimeSpan?
的类型映射:
SqlMapper.RemoveTypeMap(typeof(TimeSpan));
SqlMapper.RemoveTypeMap(typeof(TimeSpan?));
SqlMapper.AddTypeHandler(new TimeSpanToTicksHandler());
public class TimeSpanToTicksHandler : SqlMapper.TypeHandler<TimeSpan>
{
public override TimeSpan Parse(object value)
{
return new TimeSpan((long)value);
}
public override void SetValue(IDbDataParameter parameter, TimeSpan value)
{
parameter.Value = value.Ticks;
}
}
使用 Dapper 1.50.5。