gpt4 book ai didi

c# - Fluent Nhibernate 将 MySQL Time(6) 映射到 C# DateTime 的问题

转载 作者:可可西里 更新时间:2023-11-01 07:45:09 34 4
gpt4 key购买 nike

我有一个具有以下架构的 MySql 表

Field   Type            Null    Key Default Extra
id int(11) NO PRI NULL auto_increment
Date date YES MUL NULL
Time time(6) NO MUL NULL
Exch varchar(45) YES MUL NULL
ProdType varchar(45) YES NULL
Product varchar(45) YES NULL
Contract varchar(45) YES NULL
Direction varchar(45) YES NULL
Price decimal(10,4) YES NULL
Quantity int(11) YES NULL

流畅模型:

public class Trade
{
public virtual int Id { get; set; }
public virtual DateTime Date { get; set; }
public virtual DateTime Time { get; set; }
public virtual string Contract { get; set; }
public virtual string Direction { get; set; }
public virtual double Price { get; set; }
public virtual int Quantity { get; set; }
}

和映射:

public TradeMap()
{
Id(x => x.Id).Column("id");
Map(x => x.Date).Column("Date");
Map(x => x.Time).Column("Time").CustomType("timestamp");;
Map(x => x.Contract).Column("Contract");
Map(x => x.Direction).Column("Direction");
Map(x => x.Price).Column("Price");
Map(x => x.Quantity).Column("Quantity");
Table("ts");
}

我正在使用以下代码测试 ORM

        DateTime dayStart = Convert.ToDateTime("11:31:00.000000");
DateTime dayEnd = Convert.ToDateTime("11:32:00.000000");

IQueryable<Trade> result = from ts in repo.GetList<Trade>()
where ts.Date == new DateTime(2013,7,1)
&& ts.Time >= dayStart
&& ts.Time <= dayEnd
&& ts.Contract == "Sep13"
select ts;


foreach (var l in result)
{
DateTime k = l.Time;
}

Trade result = repo.GetList<Trade>().FirstOrDefault();

但我不断收到内心的异常

{"Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'."}

我尝试通过将时间映射更改为

来解决这个问题
Map(x => x.Time).Column("tsTime").CustomType("timestamp").CustomSqlType("TIME(6)").Nullable();

Map(x => x.Time).Column("tsTime").CustomSqlType("TIME(6)");

但没有任何作用

最佳答案

对 DATE + TIME 使用单个字段。

关于c# - Fluent Nhibernate 将 MySQL Time(6) 映射到 C# DateTime 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706482/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com