The following model has inconsistent behaviour when queried via LINQ with entities:
public class Test
{
public Test()
{
Children = new List<Test>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey(nameof(ParentId))]
public ICollection<Test> Children { get; set; }
public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
public Test Parent { get; set; }
[NotMapped]
public string Path
{
//get { return "Test"; }
get { return Parent != null ? Parent.Path + '.' + Name : Name; }
}
}
Given a simple Context like this:
public class MyContext : DbContext
{
public DbSet<Test> Tests { get; set; }
}
And then adding some data to it (in my case, like this):
Test t = new Test()
{
Text = "Child",
};
Test p = new Test()
{
Text = "Parent",
Children = new List<Test>() { t }
};
t.Parent = p;
ctx.Tests.Add(p);
ctx.SaveChanges();
And then restarting the application (so the Query/Object-Cache is cleared) and then querying like so:
foreach (Test test in ctx.Tests.Where(x => x.Text == "Child"))
{
Console.WriteLine("Is the parent null?: " + (test.Parent == null).ToString());
}
Outputs "true" for the added "Child" object.
However, querying like so:
Console.WriteLine("=== ALL ELEMENTS ===");
foreach (Test test in ctx.Tests)
{
Console.WriteLine("Found t with path " + test.Path);
}
Console.WriteLine("=== FILTERED ELEMENTS ===");
foreach (Test test in ctx.Tests.Where(x => x.Text == "Child" && x.Parent != null))
{
Console.WriteLine("Is the parent null?: " + (test.Parent == null).ToString());
}
Outputs "false" for the added "Child" object.
I tried this on a MySql Database, with both the Devart MySql Provider, and Oracles MySql Provider, and got the same behaviour on both, which leads me to think this is an issue with EntityFramework.
Note that test.ParentId is set to its correct value, just the property test.Parent does not get populated unless you first load the parent objects into the cache (ie, the caching mechanism causes a change in behaviour of population).
I find this behaviour really odd, I'm not sure if this is a bug, but it is very weird that the cache has effects like that.
Best Regards
public class Test
{
public Test()
{
Children = new List<Test>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey(nameof(ParentId))]
public ICollection<Test> Children { get; set; }
public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
public Test Parent { get; set; }
[NotMapped]
public string Path
{
//get { return "Test"; }
get { return Parent != null ? Parent.Path + '.' + Name : Name; }
}
}
Given a simple Context like this:
public class MyContext : DbContext
{
public DbSet<Test> Tests { get; set; }
}
And then adding some data to it (in my case, like this):
Test t = new Test()
{
Text = "Child",
};
Test p = new Test()
{
Text = "Parent",
Children = new List<Test>() { t }
};
t.Parent = p;
ctx.Tests.Add(p);
ctx.SaveChanges();
And then restarting the application (so the Query/Object-Cache is cleared) and then querying like so:
foreach (Test test in ctx.Tests.Where(x => x.Text == "Child"))
{
Console.WriteLine("Is the parent null?: " + (test.Parent == null).ToString());
}
Outputs "true" for the added "Child" object.
However, querying like so:
Console.WriteLine("=== ALL ELEMENTS ===");
foreach (Test test in ctx.Tests)
{
Console.WriteLine("Found t with path " + test.Path);
}
Console.WriteLine("=== FILTERED ELEMENTS ===");
foreach (Test test in ctx.Tests.Where(x => x.Text == "Child" && x.Parent != null))
{
Console.WriteLine("Is the parent null?: " + (test.Parent == null).ToString());
}
Outputs "false" for the added "Child" object.
I tried this on a MySql Database, with both the Devart MySql Provider, and Oracles MySql Provider, and got the same behaviour on both, which leads me to think this is an issue with EntityFramework.
Note that test.ParentId is set to its correct value, just the property test.Parent does not get populated unless you first load the parent objects into the cache (ie, the caching mechanism causes a change in behaviour of population).
I find this behaviour really odd, I'm not sure if this is a bug, but it is very weird that the cache has effects like that.
Best Regards