我最近將我的應用程式更新為:Vs 2019、EntityFramework.6.4.4、EntityFramework6.Npgsql.6.4.3 和 Npgsql.6.0.4
之前,一切都運行良好。更新后,我測驗了在資料庫中插入和讀取的不同用例(postgreSql V13)。
在插入一個帶有 timestampTz 值的簡單物體時,我得到了以下例外:
Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone', consider using 'timestamp with time zone'. 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.
這是我使用的代碼片段。物體類(POCO):
[DataContract]
[Table("TSHH_ShiftHistory", Schema = "ppc")]
public class TSHH_ShiftHistory
{
[DataMember]
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SHH_RecordingOrder { get; set; }
//..
[DataMember]
public DateTime? SHH_Started_UTC { get; set; }
[DataMember]
public DateTime? SHH_Finished_UTC { get; set; }
// ..
}
并寫入資料庫:
shiftHistory = new TSHH_ShiftHistory();
shiftHistory.SHH_Started_UTC = dateTimeStarted; // in UTC format
shiftHistory.SHH_Finished_UTC = dateTimeFinished; // in UTC format
db.TSHH_ShiftHistory.Add(shiftHistory);
int sc = db.SaveChanges();
以下是我試圖尋求幫助的網站:
https://www.npgsql.org/doc/release-notes/6.0.html#timestamp-rationalization-and-improvements https://www.npgsql.org/doc/types/datetime.html#timestamps-and-timezones https://www.npgsql.org/doc/api/NpgsqlTypes.NpgsqlDbType.html
Npgsql EF 6:使用 ExecuteSqlInterpolatedAsync 時時間戳失敗 https://github.com/npgsql/npgsql/issues/2669 https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=資料注釋#the-valueconverter-class
解決方案:
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
當然,我在文章中讀到了它,但是當我想要從 npgsql 6.0.0 開始使用時區映射的時間戳的新行為時,我沒有看到需要背單詞。但它來了。我嘗試將開關設定為true和false。令人驚訝的是,與設定值無關,沒有引發例外。
至少我取消了“AppContext.SetSwitch(..);”這一行的注釋。并且在插入 UTC DateTime 值時再次引發例外。
我的懷疑:我想知道,如果缺少“AppContext.SetSwitch(..);” 陳述句是未初始化的行為,并導致不可預測的結果。
如果有人能證實我的嫌疑人,我會很高興,也許是@Shay Rojansky?
uj5u.com熱心網友回復:
您正在使用舊的(非核心)EF6,它很可能也必須進行調整,以便考慮新的時間戳行為(EF Core 提供程式肯定會這樣做)。不幸的是,這不會發生,因為 EF6 不再被開發。
我一般建議不要使用較新版本的 Npgsql 運行 EF6;這些組合未經測驗,可能會以各種方式失敗。隨著 EF6 被有效凍結,我建議也保留相同版本的 Npgsql。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472589.html
標籤:PostgreSQL 实体框架 约会时间 npgsql
上一篇:將js日期時間轉換為時刻
下一篇:在R中分組日期以創建患者事件?
