Skip to content

Separate methods for raw SQL that leverage interpolated strings #10996

Closed

Description

@divega

Currently the behavior is different if you call FromSql or ExecuteSqlCommand with an inline interpolated string (parameterization happens) or if you assign the interpolated string to a variable and then pass it to the method (the string is formatted with the arguments).

In some cases customers expect parameterization but it doesn't happen: #10080

In other cases customers expect parameterization not to happen, but it does happen: #10956

This is very confusing and hard to explain. Interpolated strings in C# were simply not designed in a way that works well with having two overloads of the same method in which one works with interpolated strings and the other one with a regular string.

I think we should separate the methods, but we can only do this in major release because it is a breaking change.

This would also imply removing the RawSqlString type.

Activity

  1. changed the title [-]Separate methods for raw SQL methods that leverage interpolated strings[/-] [+]Separate methods for raw SQL that leverage interpolated strings[/+] on Feb 16, 2018
  2. NickCraver commented on Feb 16, 2018

    @NickCraver
    Member

    Absolutely agree with this move. If it helps, I wrote up some more test cases on how this is confusing (and sometimes dangerous) when it first came up here: https://github.com/NickCraver/EFCoreInjectionSample/blob/master/Program.cs

  3. added this to the milestone on Feb 16, 2018
  4. divega commented on Feb 17, 2018

    @divega
    ContributorAuthor

    @NickCraver thanks. We are going to try to address #10080 in the shorter term with a Roslyn analyzer. I think we can probably leverage your tests as tests cases for the analyzer.

    cc @anpete

  5. modified the milestones: , 3.0.0 on May 18, 2018
  6. ajcvickers commented on May 18, 2018

    @ajcvickers
    Contributor

    And remove or modify the analyzer?

  7. divega commented on May 19, 2018

    @divega
    ContributorAuthor

    I believe modify. I haven’t thought through all the details but I believe it can still provide value on the string overload.

  8. 35 remaining items

  9. added a commit that references this issue on Mar 14, 2019
    a999c41
  10. added this to the 3.0.0 milestone on Mar 14, 2019
  11. NickCraver commented on Mar 14, 2019

    @NickCraver
    Member

    @jeremycook my apologies - you wrote FormattableString and my brain read it as IFormattableString even after a few passes. I completely agree on the concrete type: there's no way I can think of to break that approach. If we allowed the interface anywhere...oh yeah, lots of fun. Sorry about that!

  12. ajcvickers commented on Mar 31, 2019

    @ajcvickers
    Contributor

    Closing since this is now done and the break is documented.

  13. modified the milestones: 3.0.0, on Apr 15, 2019
  14. modified the milestones: , 3.0.0 on Nov 11, 2019
  15. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions