Skip to content

Instantly share code, notes, and snippets.

@NinoFloris

NinoFloris/.cs Secret

Created March 22, 2017 01:54
Show Gist options
  • Save NinoFloris/2c91585c047050e00e388e94edc09a98 to your computer and use it in GitHub Desktop.
Save NinoFloris/2c91585c047050e00e388e94edc09a98 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace Repro
{
public class Program
{
public static void Main()
{
var db = GetContext();
var inmemcheck = new InMemoryCheck();
var variableName = "test";
var differentVariableName = "test";
// True (or any other expression translatable to SQL) seems required to get it into the ShaperCommandContext
DbContext.Set<Entity>().Where(e => e.Id == 4).Where(e2 => inmemcheck.Check(variableName, e2.Id.ToString()) || true).Count();
DbContext.Set<Entity>().Where(e => e.Id == 4).Where(e2 => inmemcheck.Check(differentVariableName, e2.Id.ToString()) || true).Count();
// No exception if var (variableName) is the same
// DbContext.Set<Entity>().Where(e => e.Id == 4).Where(e2 => inmemcheck.Check(variableName, e2.Id.ToString()) || true).Count();
// DbContext.Set<Entity>().Where(e => e.Id == 4).Where(e2 => inmemcheck.Check(variableName, e2.Id.ToString()) || true).Count();
}
private static DbContext DbContext { get; } = GetContext();
private static DbContext GetContext()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var db = new DbContext(new DbContextOptionsBuilder<DbContext>().UseSqlite(connection).Options);
db.Database.EnsureCreated();
db.Entities.AddRange(new List<Entity> { new Entity { Id = 1 }, new Entity { Id = 3 } });
db.SaveChanges();
return db;
}
}
public class Entity
{
public int Id { get; set; }
}
public class DbContext : Microsoft.EntityFrameworkCore.DbContext
{
public DbSet<Entity> Entities { get; set; }
public DbContext(DbContextOptions<DbContext> options) : base(options) { }
}
public class InMemoryCheck
{
private Dictionary<string, string> _data = new Dictionary<string, string>();
public bool Check(string input1, string input2)
=> _data.ContainsKey(input1) && _data[input1] == input2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment