我有一個包含一列的表,每當我在表中執行插入或更新操作時,我想在此列LastUpdateOn中保留 SQL Server 當前日期和時間(使用)。GETDATE()
在 ADO.NET 中,我們曾經在插入/更新陳述句中呼叫GETDATE()函式,但是我們如何使用 Entity Framework Core 來實作相同的功能呢?
uj5u.com熱心網友回復:
我有兩個快速解決方案:
簡單地將值作為默認值設定為該列上的 GetDate() 并在插入或更新時將 null 傳遞給 EF;
創建一個在插入/更新之前呼叫 GetDate() 的函式并將其用作值;
編輯:
- 在插入/更新之前為獲取 GetDate() 函式的 DateTime 值創建一個新的 EF 配置并自動執行該程序;
uj5u.com熱心網友回復:
因此,當我遠離存盤程序(我經常使用“GetDate()”或“CURRENT_TIMESTAMP”)......我接受了 c#/middleware 層。
我做了一個“空檢查”并將其設定在 c# 代碼中(在物體框架資料層代碼中)
下面顯示日期時間。DateTime.MinValue 的意思是“有人沒有明確設定它”。
myEntityFrameworkEntity.InsertDate = parameterInsertDateValue == DateTime.MinValue ? DateTime.Now : parameterInsertDateValue;
還有其他變化,但想法是一樣的。“呼叫者”是否明確設定了屬性,或者他們沒有設定它,讓我代表他們寫一個值。
現在,有時我不在乎呼叫者設定了什么。如果它是 LastUpdatedDate ,您也可以“強制它”(始終將其設定為 DateTime.Now)......
// i don't care what the parameters are, i always update it in this layer
myEntityFrameworkEntity.LastUpdatedDate = DateTime.Now;
此外,而不是直接使用 DateTime(.Now) ......我使用這個:
https://github.com/vfabing/Chronos.Net
您“注入”了一個可以為單元測驗模擬的 DateTime-PROVIDER ..。
這是一個更完整的示例來顯示我的 EF 代碼(對于需要“斷開連接”物體的 Rest-Services)。
public const int ExpectedAddSingleOrUpdateSingleOrDeleteSingleSaveChangesAsyncRowCount = 1;
public async Task<EmployeeEfEntity> UpdateAsync(
EmployeeEfEntity inputEmployee,
CancellationToken token)
{
int saveChangesAsyncValue = 0;
/* find the object in the db-context using the object surrogate-primary-key (since this is an update) */
EmployeeEfEntity foundEntity =
await this.entityDbContext.EmployeeEfEntitys.FirstOrDefaultAsync(
item => item.EmployeeEfEntityKey == inputEmployee.EmployeeEfEntityKey,
token);
if (null != foundEntity)
{
/* update the found-entity with the properties of the input object */
/* the "normal" properties of the employee */
foundEntity.EmployeeBadgeNumber = inputEmployee.EmployeeBadgeNumber;
/* if the insert-date was explicit, use it, otherwise default to date-time.now */
foundEntity.InsertDate = inputEmployee.InsertDate == DateTime.MinValue ? DateTime.Now : inputEmployee.InsertDate;
/* we do not care about the input parameters, we always set it to "now" */
foundEntity.LastUpdated = inputEmployee.LastUpdated == DateTime.Now;
this.entityDbContext.Entry(foundEntity).State = EntityState.Modified;
saveChangesAsyncValue = await this.entityDbContext.SaveChangesAsync(token);
if (ExpectedAddSingleOrUpdateSingleOrDeleteSingleSaveChangesAsyncRowCount != saveChangesAsyncValue)
{
throw new ArgumentOutOfRangeException(
string.Format(ErrorMsgExpectedSaveChangesAsyncRowCount, saveChangesAsyncValue),
(Exception)null);
}
}
else
{
ArgumentOutOfRangeException argEx = new ArgumentOutOfRangeException(
string.Format(ErrorMsgPrimaryEntityNotFound, inputEmployee.EmployeeEfEntityKey),
(Exception)null);
this.logger.LogError(argEx);
}
return foundEntity;
}
我的代碼實際上使用了 Chronos.Net(注入)日期時間提供程式,但我洗掉了它以使代碼更簡單地回答這個問題。
uj5u.com熱心網友回復:
由于您使用的是物體框架,該框架旨在允許更多地在 C# 中處理資料庫使用,因此我將只使用 DateTime.Now(如果您想要 UTC 時間,則使用 DateTime.UtcNow)。
設定LastUpdateOn為DateTime.Now或DateTime.Utc.Now每次輸入/更新記錄。
如果你真的想使用 SQL server 的GETDATE()方法,那么撰寫一個存盤程序,它需要一個整數(要更新的記錄的 id),并用它來LastUpdateOn為具有指定 id 的欄位設定GETDATE().
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486097.html
