Skip to content

Translate {IPAddress,NetworkAddress}.Parse() #534

Closed

Description

@roji

@austindrenski, in addition to all the great translations you've recently added, I think we're missing one: IPAddress.Parse(), which is basically the way to specify an IP address literal...

This should be pretty easy, feel like doing it?

Activity

  1. added this to the 2.2.0 milestone on Jul 22, 2018
  2. austindrenski commented on Jul 22, 2018

    @austindrenski
    Contributor

    Sure! Should I include PhysicalAddress.Parse() at the same time?

  3. changed the title [-]Translate IPAddress.Parse()[/-] [+]Translate {IPAddress,NetworkAddress}.Parse()[/+] on Jul 22, 2018
  4. roji commented on Jul 22, 2018

    @roji
    MemberAuthor

    Absolutely!

  5. austindrenski commented on Jul 22, 2018

    @austindrenski
    Contributor

    See #535 for implementations + tests.

    I ran into some trouble along the way with EF Core preempting the translators by transforming the .Parse(string) calls and replacing IPAddress.Parse('127.0.0.1') with @__Parse_0 (value: '127.0.0.1').

    I got around this by modifying NpgsqlEvaluatableExpressionFilter, but I'm not sure that I fully understand what this class is meant to do. Obviously it marks methods that are not evaluable, but there are many methods for which we don't use this class, and which we don't have the problem described above.

    The action seems to be happening in in ParameterExtractingExpressionVisitor which is called from QueryModelGenerator. It replaces the entire call (IPAddress.Parse("127.0.0.1")) with a parameter whose value is just a constant ('127.0.0.1').

    @roji Do you have any insight into how this part of the EF Core pipeline is supposed to work?

  6. roji commented on Jul 23, 2018

    @roji
    MemberAuthor

    NpgsqlEvaluatableExpressionFilter is only meant to specify methods that should not be evaluated on the client, ever. This is a way to avoid evaluating things like DateTime.Now client-side (because time zones may be different), or creating a new GUID client-side (which EF Core would reuse across different database rows, rather than generating a GUID per-row). If you're interested in reading more about this, see the discussion in dotnet/efcore#12552 (comment) (including possible upcoming changes).

    For our purposes... if the expression contains IPAddress.Parse("127.0.0.1") (with a constant!), then it does make sense for EF Core to evaluate it locally and send the result as a parameter (this is exactly what NpgsqlEvaluatableExpressionFilter is meant to prevent) - there's no reason to avoid it. The only important bit is to make sure that the parameter really does contain the proper instance of IPAddress - it's not clear from your comments if that's happening or not.

    If, however, the expression contains a non-constant (e.g. some text column in the database, or a C# variable), then EF Core cannot do client evaluation as above, and you translator should get invoked as expected. Here are some examples:

    var text = "127.0.0.1";
    // With variable
    var blogs = ctx.Blogs.Where(b => b.SomeIp == IPAddress.Parse(text)).ToList();
    // With database column
    var blogs = ctx.Blogs.Where(b => b.SomeIp == IPAddress.Parse(b.SomeTextColumn)).ToList();

    So what you're seeing is probably simply the expected correct behavior, let me know if otherwise.

  7. austindrenski commented on Jul 23, 2018

    @austindrenski
    Contributor

    Ahhh. That all makes sense. I saw some unexpected parameterization and panicked assumed the worst!

    What's interesting (and caught me off-guard) is that both of the following are parameterized:

    src.Where(x => x.Inet == IPAddress.Parse("127.0.0.1"));
    // WHERE x."Inet" = @__Parse_0
    
    string value = "127.0.0.1"
    src.Where(x => x.Inet == IPAddress.Parse(value));
    // WHERE x."Inet" = @__Parse_0

    If you're interested in reading more about this, see the discussion in dotnet/efcore#12552 (comment) (including possible upcoming changes).

    Much appreciated!

  8. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions