Skip to content

Instantly share code, notes, and snippets.

@mabster
Created September 26, 2019 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabster/dd0e7cc7041ba42ce26725838a1d1180 to your computer and use it in GitHub Desktop.
Save mabster/dd0e7cc7041ba42ce26725838a1d1180 to your computer and use it in GitHub Desktop.
EF Core query translation fails with internal properties
using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main()
{
var options = new DbContextOptionsBuilder();
options.UseInMemoryDatabase("Widgets");
using var db = new WidgetContext(options.Options);
db.Set<Widget>().AddRange(
new Widget { Name = "Foo" },
new Widget { Name = "Bar" },
new Widget { Name = "Fizz", Tag = "fizz" }
);
await db.SaveChangesAsync();
var query = db.Set<Widget>().Where(w => w.Tag == "fizz");
foreach (var w in await query.ToListAsync())
{
Console.WriteLine(w.Name);
}
}
}
public class Widget
{
public int Id { get; set; }
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
internal string Tag { get; set; }
}
class WidgetContext : DbContext
{
public WidgetContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Widget>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment