從 Oracle 的“DUAL”獲取 SYSDATE 的最佳方法是什么?
我嘗試使用 ExecuteSqlRawAsync 但我也需要回傳值。
public async Task<DateTime> GetTime()
{
// This does not return value and does not work
await _dbContext.Database.ExecuteSqlRawAsync("Select sysdate from dual;");
}
另一種方法是創建一個模型構建器。
public partial class Dual
{
public DateTime SysDate { get; set; }
}
public virtual DbSet<Dual> Dual { get; set; }
modelBuilder.Entity<Dual>(e => { ... });
并使用查詢。
await _dbContext.Dual.FromSqlRaw("Select sysdate from dual;").ToListAsync();
有沒有更簡單或更好的方法可以從 Oracle 獲取“SYSDATE”?我正在使用 EF Core 版本 5。
uj5u.com熱心網友回復:
您可以嘗試此執行緒中提到的解決方案:
物體框架 7:獲取資料庫時間
public async Task<DateTime> GetTime()
{
DbConnection connection = _dbContext.Database.GetDbConnection();
if (connection.State == ConnectionState.Closed)
{
await connection.OpenAsync();
}
var command = connection.CreateCommand();
command.CommandText = "select sysdate from dual";
return (DateTime) await command.ExecuteScalarAsync();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347541.html
上一篇:從注冊表項路徑中提取根配置單元
