使用语言C#
使用框架.NET Core 6
使用数据库postgreSQL
在使用ef core查询sql,以时间类型为条件,查询报错;
错误信息:
Cannot write DateTime with Kind=Unspecified to PostgreSQL type ‘timestamp with time zone’, only UTC is supported. Note that it’s not possible to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.
解决办法,主要是把日期数据转为UTC类型的日期:
var date = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + "05:00:00" );
DateTime utcDate=DateTime.SpecifyKind(date, DateTimeKind.Utc);
//ef core原始sql执行
int count3 = await db.Database.ExecuteSqlInterpolatedAsync($"delete from rs_device_report_file_processed where file_processed_time>{utcDate}");