You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When fetching an entity which does not have a parent entity, including the parent removes the entity from the result. It worked correctly in 2.0 and fails since migrating to 2.2.
varoptions=newDbContextOptionsBuilder<OrderContext>().UseInMemoryDatabase(databaseName:"Add_writes_to_database").Options;using(varcontext=newOrderContext(options)){// prepare parent entityvarorder=newOrder(){Id=1};context.Add(order);context.SaveChanges();// prepare child without parentvarorderItemWithoutParent=newOrderItem(){Id=1};context.Add(orderItemWithoutParent);context.SaveChanges();// prepare child with parentvarorderItemWithParent=newOrderItem(){Id=2,Order=order};context.Add(orderItemWithParent);context.SaveChanges();}using(varcontext=newOrderContext(options)){// OK with Include if entity has ParentvarorderItemWithParent=context.OrderItems.Include(o =>o.Order).FirstOrDefault(o =>o.Id==2);Assert.NotNull(orderItemWithParent);// OK for entity without parent without IncludevarorderItemWithoutParent=context.OrderItems.FirstOrDefault(o =>o.Id==1);Assert.NotNull(orderItemWithoutParent);// FAILS for entity without parent because of IncludeorderItemWithoutParent=context.OrderItems.Include(o =>o.Order).FirstOrDefault(o =>o.Id==1);Assert.NotNull(orderItemWithoutParent);}
I found out more: This behaviour is limited to relationships, where the FK-Property is not nullable:
This will FAIL:
public class OrderItem
{
public int Id { get; set; }
public Order Order { get; set; }
public int OrderId { get; set; } // <= int
}
This will SUCCEED:
public class OrderItem
{
public int Id { get; set; }
public Order Order { get; set; }
public int? OrderId { get; set; } // <= int?
}
This is annoying, as the advantage of using the InMemory provider is not to have to keep up referential integrity. I don't want to make all my FKs nullable to make my tests run again.
When fetching an entity which does not have a parent entity, including the parent removes the entity from the result. It worked correctly in 2.0 and fails since migrating to 2.2.
Steps to reproduce
Repro project is attached Demo.zip
The text was updated successfully, but these errors were encountered: