-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed as not planned
Closed as not planned
Copy link
Labels
Description
I would expect the following code returns the instance of Child (the same instances).
But it depends on a query filter. If child's master is filtered, the second line returns null.
Child child = dbContext.Set<Child>().Where(c => c.Id == 1).FirstOrDefault();
Child childInludingMaster = dbContext.Set<Child>().Include(m => m.Master /* has a query filter */).Where(c => c.Id == 1).FirstOrDefault();
Steps to reproduce
- Install NuGet packages
- Create database IssueDemo (or change connection string)
- Generate database migration
- Run unit test
[TestClass]
public class TestIssue
{
[TestMethod]
public void DbSet_ShouldReturnSameInstancesWheneverIncludeIsCalled()
{
// Arrange
#region Data initialization
int childId;
using (var dbContext = new MyDbContext())
{
dbContext.Database.Migrate();
Child child = new Child { IsDeleted = false };
Master master = new Master { IsDeleted = true, Children = new List<Child> { child } };
dbContext.Set<Master>().AddRange(master);
dbContext.SaveChanges();
childId = child.Id;
}
#endregion
// Act
using (var dbContext = new MyDbContext())
{
Child child = dbContext.Set<Child>().Where(c => c.Id == childId).FirstOrDefault();
Child childInludingMaster = dbContext.Set<Child>().Include(m => m.Master /* has a query filter */).Where(c => c.Id == childId).FirstOrDefault();
// Assert
Assert.AreSame(Object.ReferenceEquals(child, childInludingMaster), "Instances should be the same.");
}
}
}
#region Model
public class Child
{
public int Id { get; set; }
public Master Master { get; set; }
public int MasterId { get; set; }
public bool IsDeleted { get; set; }
}
public class Master
{
public int Id { get; set; }
public List<Child> Children { get; set; }
public bool IsDeleted { get; set; }
}
#endregion
#region MyDbContext
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Master>().HasQueryFilter(item => !item.IsDeleted);
modelBuilder.Entity<Child>().HasQueryFilter(item => !item.IsDeleted);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=IssueDemo;Trusted_Connection=True;");
}
}
#endregion
Further technical details
EF Core version: 2.0.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Target .NET Framework: 4.6.1