Created
September 26, 2019 03:04
-
-
Save mabster/dd0e7cc7041ba42ce26725838a1d1180 to your computer and use it in GitHub Desktop.
EF Core query translation fails with internal properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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