我无法让Dapper与MySQL数据库DATETIME
类型一起工作。对于"01-01-0001 00:00:00"
列,它始终返回DATETIME
。以下是一些细节:
.NET:
DNX 4.5.1
MysqlData 6.9.8
Dapper - 1.50.0-beta6
MySQL: Server Version: 5.5.43-0+deb8u1
表:
CREATE TABLE campaign (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
...
creation_date datetime DEFAULT NULL,
last_modified datetime DEFAULT NULL,
...
)
ENGINE = INNODB
C#:
public class Campaign
{
public int Id { get; set; }
public DateTime CreationDate {get; set; }
public DateTime LastModified { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
...
const string myConnectionString = "server=[IP];uid=[usr];pwd=[pass];database=[db];AllowZeroDatetime=false;";
try
{
_conn = new MySqlConnection { ConnectionString = myConnectionString };
_conn.Open();
}
...
public Dictionary<int, Campaign> GetCampaigns()
{
var campaignsDict = _conn.Query<Campaign>("select * from campaign").ToDictionary(row => row.Id,row => row);
return campaignsDict;
}
这是检索到的行在campaignsDict中的内容:
P.S。我只是在学习.NET,所以我是这项技术的新手。
答案 0 :(得分:2)
Dapper不会自动删除下划线,除非通过以下方式通知:
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
因此,您实际拥有的是尚未从列映射的属性。 DateTime
的默认值为:01-01-0001 00:00:00。
选项:
答案 1 :(得分:0)
更改模型,以便日期列是可选的,例如:
public class Campaign
{
public int Id { get; set; }
public DateTime? CreationDate {get; set; }
public DateTime? LastModified { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
}