EF Core 2.2-如何使用时区SQL片段创建SQLEXPRESSION
我想将方法映射到自定义SQL,所以
CONVERT(datetime,
SWITCHOFFSET(GETUTCDATE(), DATEPART(TZOFFSET, GETUTCDATE() AT TIME ZONE 'Greenwich Standard Time')))
我在我的dbcontext
中添加了hasdbfunction
:
modelBuilder.HasDbFunction(typeof(DbContext).GetMethod(nameof(DbContext.GetDateTime)))
.HasTranslation(e =>
{
var GETUTCDATE = new SqlFunctionExpression("GETUTCDATE", typeof(DateTime));
var TZOFFSET = new SqlFragmentExpression("TZOFFSET");
var ATTIMEZONE = new SqlFragmentExpression(" AT TIME ZONE ");
var GreenwichStandardTime = new Expression("Greenwich Standard Time"); // expression with constant?
var EXPRESSION = new Expression(GETUTCDATE, ATTIMEZONE, GreenwichStandardTime); // expression with combine multiple exprssions?
var DATEPART = new SqlFunctionExpression("DATEPART", typeof(int), new[] { TZOFFSET , EXPRESSION }); // cannot create
var SWITCHOFFSET = new SqlFunctionExpression("SWITCHOFFSET", typeof(DateTimeOffset), new[] { GETUTCDATE, DATEPART });
var DATETIME = new SqlFragmentExpression("DATETIME");
new SqlFunctionExpression("CONVERT", typeof(DateTime), new[] { DATETIME, SWITCHOFFSET });
});
我的问题是如何创建greenwichstandardtime和expression?
I want to map method to custom SQL, which is
CONVERT(datetime,
SWITCHOFFSET(GETUTCDATE(), DATEPART(TZOFFSET, GETUTCDATE() AT TIME ZONE 'Greenwich Standard Time')))
So I added HasDbFunction
in my DbContext
:
modelBuilder.HasDbFunction(typeof(DbContext).GetMethod(nameof(DbContext.GetDateTime)))
.HasTranslation(e =>
{
var GETUTCDATE = new SqlFunctionExpression("GETUTCDATE", typeof(DateTime));
var TZOFFSET = new SqlFragmentExpression("TZOFFSET");
var ATTIMEZONE = new SqlFragmentExpression(" AT TIME ZONE ");
var GreenwichStandardTime = new Expression("Greenwich Standard Time"); // expression with constant?
var EXPRESSION = new Expression(GETUTCDATE, ATTIMEZONE, GreenwichStandardTime); // expression with combine multiple exprssions?
var DATEPART = new SqlFunctionExpression("DATEPART", typeof(int), new[] { TZOFFSET , EXPRESSION }); // cannot create
var SWITCHOFFSET = new SqlFunctionExpression("SWITCHOFFSET", typeof(DateTimeOffset), new[] { GETUTCDATE, DATEPART });
var DATETIME = new SqlFragmentExpression("DATETIME");
new SqlFunctionExpression("CONVERT", typeof(DateTime), new[] { DATETIME, SWITCHOFFSET });
});
My question is how can I create GreenwichStandardTime and EXPRESSION?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
continue
From @Charlieface we can create expression from SqlFragmentExpression
in case I want to replace 'Greenwich Standard Time' with function parameter as @Charlieface use
[NotParameterized] will not convert it to parameter on expression