1. 使用 EF Scaffolding 指令去更新 Model ,有些欄位原本是明確型別 變成 可null型別
也就是本來是 datetime 變成 datetime?, int 變成 int?
看起來是一些 view 的欄位會有這些狀況,這時就要去 git revert ,避免build專案又一堆錯誤
2. 使用 IQueryable 語法處理時間要小心,不要在裡面計算時間。
DateTime shouldBeGiveCoinDateTime = DateTime.Now.AddDays(-14);
return dbContext.ViewOrderForCoinCronJob.AsNoTracking().Where(x =>
x.CompletedAt <= shouldBeGiveCoinDateTime
&& x.CreatedAt >= DateTime.Now.AddYears(-1) // -> 這裡轉換mysql指令會出錯
&& x.DeletedAt == null
&& x.CoinAmount > 0
&& x.Status == (byte)ORDER_STATUS.COMPLETED
&& dbContext.PackingMain.Any(y => y.OrderId == x.Id)
&& x.IsGiveCoin != 0
&& !dbContext.CoinLog.Any(y =>
y.OrderId == x.Id
&& y.StrKey.Contains(COIN_LOG.STRING_KEY_PREFIX.ORDER_ACCUMULATION)
)
).ToList();
要把時間計算先算出來,再帶入式子
DateTime shouldBeGiveCoinDateTime = DateTime.Now.AddDays(-14);
DateTime createDateStartAt = DateTime.Now.AddYears(-1); // -> 先算出日期
return dbContext.ViewOrderForCoinCronJob.AsNoTracking().Where(x =>
x.CompletedAt <= shouldBeGiveCoinDateTime
&& x.CreatedAt >= createDateStartAt // -> 帶入算好的日期
&& x.DeletedAt == null
&& x.CoinAmount > 0
&& x.Status == (byte)ORDER_STATUS.COMPLETED
&& dbContext.PackingMain.Any(y => y.OrderId == x.Id)
&& x.IsGiveCoin != 0
&& !dbContext.CoinLog.Any(y =>
y.OrderId == x.Id
&& y.StrKey.Contains(COIN_LOG.STRING_KEY_PREFIX.ORDER_ACCUMULATION)
)
).ToList();