-
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
ThenInclude fails with ArgumentNullException for the navigation of base class which is not added as entity type #3994
Comments
Repro'd on the latest bits. Seems to be related to having a navigation defined on an unmapped base type. Below is a runnable repro. If BaseClass is pulled into the model, then the query succeeds. It's also not as basic as just including any nav prop on an unmapped base type, as querying for ClassB and Including CItems works fine. using Microsoft.Data.Entity;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new SampleContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var a = new ClassA
{
BItems = new List<ClassB>
{
new ClassB
{
CItems = new List<ClassC>
{
new ClassC()
}
}
}
};
db.Add(a);
db.Add(a.BItems.Single());
db.Add(a.BItems.Single().CItems.Single());
db.SaveChanges();
}
using (var db = new SampleContext())
{
var id = 1;
var a = db.Set<ClassA>()
.Include(x => x.BItems)
.ThenInclude(x => x.CItems)
.SingleOrDefaultAsync(x => x.Id == id);
}
}
}
public class SampleContext : DbContext
{
public DbSet<ClassA> ClassAs { get; set; }
public DbSet<ClassB> ClassBs { get; set; }
public DbSet<ClassC> ClassCs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
builder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Samples;Trusted_Connection=True;");
}
}
public class ClassA
{
public int Id { get; set; }
public ICollection<ClassB> BItems { get; set; }
}
public class ClassB : BaseClass
{
}
public class BaseClass
{
public int Id { get; set; }
public ICollection<ClassC> CItems { get; set; }
}
public class ClassC
{
public int Id { get; set; }
}
} |
The issue is when we work with chained navigations, we find the declaring type in our model to get the navigation from property info. If the navigation is declared on base type which is not included in the model then navigation is not found and ArgumentNullException is thrown. |
I have the following classes
When I try to execute the following statement it fails with ArgumentNullException
Here is the stack trace
EDIT: It also failed when I tried to make GenericClass non-generic. I guess it happened because of CItems collection
The text was updated successfully, but these errors were encountered: