Skip to content
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

Include not working properly with InMemoryDatabase #14462

Closed
stefan-landgraf-is24 opened this issue Jan 18, 2019 · 3 comments
Closed

Include not working properly with InMemoryDatabase #14462

stefan-landgraf-is24 opened this issue Jan 18, 2019 · 3 comments

Comments

@stefan-landgraf-is24
Copy link

stefan-landgraf-is24 commented Jan 18, 2019

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.

var options = new DbContextOptionsBuilder<OrderContext>()
	.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
	.Options;

using (var context = new OrderContext(options))
{
	// prepare parent entity
	var order = new Order() { Id = 1 };
	context.Add(order);
	context.SaveChanges();

	// prepare child without parent
	var orderItemWithoutParent = new OrderItem() { Id = 1 };
	context.Add(orderItemWithoutParent);
	context.SaveChanges();

	// prepare child with parent
	var orderItemWithParent = new OrderItem() { Id = 2, Order = order };
	context.Add(orderItemWithParent);
	context.SaveChanges();
}

using (var context = new OrderContext(options))
{
	// OK with Include if entity has Parent
	var orderItemWithParent = context.OrderItems
		.Include(o => o.Order)
		.FirstOrDefault(o => o.Id == 2);

	Assert.NotNull(orderItemWithParent);

	// OK for entity without parent without Include
	var orderItemWithoutParent = context.OrderItems
		.FirstOrDefault(o => o.Id==1);

	Assert.NotNull(orderItemWithoutParent);

	// FAILS for entity without parent because of Include
	orderItemWithoutParent = context.OrderItems
		.Include(o => o.Order)
		.FirstOrDefault(o => o.Id == 1);

	Assert.NotNull(orderItemWithoutParent);
}

Steps to reproduce

Repro project is attached Demo.zip

@stefan-landgraf-is24
Copy link
Author

stefan-landgraf-is24 commented Jan 22, 2019

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.

@alekrist
Copy link

Please fix this as it is also blocking us to upgrade to v2.2

@ajcvickers
Copy link
Contributor

Duplicate of #9470 See also #2166 and #13146

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants