-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Checking for a derived type using the "is" operator no longer works #18569
Comments
@BickelLukas I have not been able to reproduce this--see my code below. Please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. abstract class Base
{
public int Id { get; set; }
}
class Derived : Base
{
public string Test { get; } = "Cheese";
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Base>();
modelBuilder.Entity<Derived>();
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Derived());
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var query = from item in context.Set<Base>()
select new
{
Test = item is Derived ? ((Derived) item).Test : "default"
};
var results = query.ToList();
}
}
} |
class Program
{
static void Main(string[] args)
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Derived("Cheese"));
context.SaveChanges();
}
using (var context = new BloggingContext())
{
var query = (from item in context.Set<Base>()
select new
{
Test = item is Derived ? ((Derived)item).Test : null,
Test2 = item is Derived,
});
var results = query.ToList(); // Test = null; test2 = false
}
}
}
public abstract class Base
{
public int Id { get; set; }
public Metadata Metadata { get; set; } = new Metadata();
}
public class Metadata
{
public DateTimeOffset Created { get; set; }
}
public class Derived : Base
{
public Derived(string test)
{
Test = test;
}
public string Test { get; private set; }
}
public class BloggingContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseInMemoryDatabase("test");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Base>().OwnsOne(x => x.Metadata);
modelBuilder.Entity<Derived>();
}
} The problem starts occurring when the base class contains an owned type. |
@BickelLukas Thanks! Note for triage: This repros for me with in-memory and the current 3.1 bits. That is, there is no exception, but the results are incorrect. However, on 3.0, this instead throws an exception, so we already changed something in 3.1 that impacted this. 3.0 exception (not thrown in 3.1)
|
When querying a DbSet of an abstract type and then checking if the materialized value is of a specific type, the check always returns null.
Steps to reproduce
When executing the query
Test
is always set todefault
Further technical details
EF Core version: 3.1.0-preview1.19508.20
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: