Efcore: Provide better error message when SQL query strings fail in string formatting

Created on 12 Jun 2017  ·  4Comments  ·  Source: dotnet/efcore

var sql=“insert into talele(data)value(‘{data:123}’);”;
this.Database.ExecuteSqlCommand(sql);

System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.FormatError()
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.Format(String format, Object[] args)
at Microsoft.EntityFrameworkCore.Storage.Internal.RawSqlCommandBuilder.Build(String sql, IReadOnlyList`1 parameters)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, String sql, Object[] parameters)

area-query type-enhancement

All 4 comments

this is bug

@gujunxiong The curly braces are interpreted as place markers for query parameters. If you want to include curly braces without them being interpreted in this way, then you'll need to escape them--something like this:
C# var sql=“insert into talele(data)value(‘{{data:123}}’);”; this.Database.ExecuteSqlCommand(sql);

Leaving this issue open since we should wrap the FormatException and provide some guidance on this.

The same problem if I have curly braces in the comment message into my SQL script.

-- some comment message {GUID}
select newId();
Was this page helpful?
0 / 5 - 0 ratings