Код: Выделить всё
// storageConfiguration and retentionPolicy are some models
string storageId = storageConfiguration.StorageId;
int retentionDays = retentionPolicy.Retention;
// This determines if "day" takes a 's' or no
string dayOrDays = $"day{(retentionDays > 1 ? "s" : string.Empty)}";
// This gives a result like "9 days"
string retentionDaysValue = $"{retentionDays} {dayOrDays}";
string command = $"SELECT add_retention_policy('{storageId}', INTERVAL '{retentionDaysValue}');"
// Now the query is executed
await dbContext.Database.ExecuteSqlRawAsync(command);
Я попробовал несколько варианты на этот счет:
Код: Выделить всё
string storageId = storageConfiguration.StorageId;
int retentionDays = retentionPolicy.Retention;
string dayOrDays = $"day{(retentionDays > 1 ? "s" : string.Empty)}";
string retentionDaysValue = $"{retentionDays} {dayOrDays}";
var storageIdParameter = new NpgsqlParameter{ParameterName = "storageId", Value = storageId};
var retentionDaysParameter = new NpgsqlParameter{ParameterName = "retentionDays", Value = retentionDaysValue};
string parameterizedCommand = "SELECT add_retention_policy(@storageId, INTERVAL @retentionDays);";
await dbContext.Database.ExecuteSqlRawAsync(parameterizedCommand, storageIdParameter, retentionDaysParameter);
42601: синтаксическая ошибка на уровне "$2" или около него
что, насколько я понимаю, означает, что второй параметр ошибочен.
Некоторые варианты, которые я пробовал:
Код: Выделить всё
// INTERVAL included in the parameter
string parameterizedCommand = "SELECT add_retention_policy(@storageId, @retentionDays);";
// "day" or "days" outside the parameter where "retentionDays" contains only a number
string parameterizedCommand = $"SELECT add_retention_policy(@storageId, INTERVAL @retentionDays '{dayOrDays}');";
Какой синтаксис следует использовать в этом случае, если я хочу параметризовать компонент RetentionDaysValue вызова ?
Подробнее здесь: https://stackoverflow.com/questions/785 ... f-the-para