Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect translation for function ContainsOrEqual in 5.0.0 #1609

Closed
heggi opened this issue Dec 16, 2020 · 3 comments
Closed

Incorrect translation for function ContainsOrEqual in 5.0.0 #1609

heggi opened this issue Dec 16, 2020 · 3 comments

Comments

@heggi
Copy link

heggi commented Dec 16, 2020

Code

db.User
.Where(m => m.Region == region)
.Where(m => EF.Functions.ContainsOrEqual(m.IPv4.Value, ipv4))
.OrderBy(m => m.Mac)
.FirstOrDefault();

Translated to

SELECT u.uid, ...
FROM "user" AS u
WHERE (u.region = @__region_0) AND u.ip@__ipv4_2
ORDER BY u.mac
FETCH FIRST 1 ROWS ONLY

Got u.ip@__ipv4_2 instead (u.ip >>= @__ipv4_2)

I could not find any error in my code :(

public class AppDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(connectionString);
        optionsBuilder.UseBatchEF();
        optionsBuilder.UseLoggerFactory(logger);
        optionsBuilder.EnableSensitiveDataLogging();
    
        base.OnConfiguring(optionsBuilder);
    }
    public DbSet<Entities.User> User { get; set; }
}

[Table("user")]
public class User
{
  [Key, Column("uid")]
  public string Uid { get; set; }

  [Column("ip")]
  public (IPAddress, int)? IPv4 { get; set; }

 ...
}
@heggi
Copy link
Author

heggi commented Dec 16, 2020

Library Zack.EFCore.Batch breaks query translation.

@roji
Copy link
Member

roji commented Dec 16, 2020

@heggi I couldn't reproduce the problem with the code below. Did you mean that Zack.EFCore.Batch is the source of the issue? If so, this should be flagged with the author of that library.

Attempted repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

var ipv4 = IPAddress.Parse("10.0.0.1");

_ = ctx.User
    // .Where(m => m.Region == region)
    .Where(m => EF.Functions.ContainsOrEqual(m.IPv4.Value, ipv4))
    // .OrderBy(m => m.Mac)
    .FirstOrDefault();

public class BlogContext : DbContext
{
    public DbSet<User> User { get; set; }

    static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .EnableSensitiveDataLogging()
            .UseLoggerFactory(ContextLoggerFactory);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

[Table("user")]
public class User
{
    [Key, Column("uid")]
    public string Uid { get; set; }

    [Column("ip")]
    public (IPAddress, int)? IPv4 { get; set; }
}

@heggi
Copy link
Author

heggi commented Dec 16, 2020

If you add .UseBatchEF(); to optionsBuilder, you will got error.
I'm create issue yangzhongke/Zack.EFCore.Batch#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants