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

Reference properties of attached entities are not considered as loaded (IsLoaded==false) #18142

Closed
jirikanda opened this issue Sep 30, 2019 · 0 comments · Fixed by #18436
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@jirikanda
Copy link

When I attach a graph of objects in EF Core 3.0, reference properties are not marked as loaded (IsLoaded is false).
DbContext tracks a root entity, it also tracks a referenced entity, so I think it should be marked as loaded. In EF 2.2 they was marked as loaded.

(Low priority for me.)

Steps to reproduce

  • Create unit test project
  • Add Microsoft.EntityFrameworkCore.InMemory
  • Add and run the following unit test
	[TestClass]
	public class IsLoadedPropertyReproSteps
	{
		[TestMethod]
		public void IsLoadedPropertyReproSteps_PropertiesOfAttachedEntityShouldBeMarkedAsLoaded()
		{
			// Arrange
			Car car = new Car
			{
				Id = 5,
				Color = new Color
				{
					Id = 60,
					Name = "Red"
				}
			};

			MyDbContext dbContext = new MyDbContext();

			// Act
			dbContext.Attach(car);

			// Assert
			Assert.AreEqual(dbContext.Entry(car).State, EntityState.Unchanged); // OK, tracked
			Assert.AreEqual(dbContext.Entry(car.Color).State, EntityState.Unchanged); // OK, tracked			

			Assert.IsTrue(dbContext.Entry(car).Navigation(nameof(Car.Color)).IsLoaded); // fails
		}
	}

	public class MyDbContext : DbContext
	{
		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);
			modelBuilder.Entity<Car>();
			modelBuilder.Entity<Color>();
		}

		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			base.OnConfiguring(optionsBuilder);

			optionsBuilder.UseInMemoryDatabase(nameof(MyDbContext));
		}
	}

	public class Car
	{
		public int Id { get; set; }
		public Color Color { get; set; }
	}

	public class Color
	{
		public int Id { get; set; }
		public string Name { get; set; }
	}

Further technical details

EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.0

@smitpatel smitpatel added this to the 3.1.0 milestone Sep 30, 2019
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 17, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0, 3.1.0-preview2 Oct 24, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0-preview2, 3.1.0 Dec 2, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants