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

Get an EntityEntry<TEntity> for Shared Entity Types #25354

Closed
Tracked by #22954
zoriya opened this issue Jul 28, 2021 · 3 comments · Fixed by #28451
Closed
Tracked by #22954

Get an EntityEntry<TEntity> for Shared Entity Types #25354

zoriya opened this issue Jul 28, 2021 · 3 comments · Fixed by #28451
Assignees
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@zoriya
Copy link

zoriya commented Jul 28, 2021

Get an EntityEntry for Shared Entity Types

Right now, it is impossible to access an EntityEntry (via Context.Entry(obj)) for a SharedEntity and this prevents us from changing states of entries.

(Not really necessary to understand the issue, but I need to access the EntityEntry of the shared entity because I want to set the state of the shared entity to Added while keeping related entities to Unchanged)

Reproducible example

private class TestEntity
{
	public int ID { get; set; }
	public ICollection<TestShared> Shared { get; set; }
}

private class TestShared
{
	public int ID { get; set; }
	public string Property { get; set; }
}

private class SharedEntityContext : DbContext
{
	public DbSet<TestEntity> Entities { get; set; }

	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		optionsBuilder.UseSqlite(":memory:");
	}

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.SharedTypeEntity<TestShared>("EntityShared")
			.HasKey(x => x.ID);
		modelBuilder.SharedTypeEntity<TestShared>("EntityShared")
			.HasOne<TestEntity>()
			.WithMany(x => x.Shared)
			.OnDelete(DeleteBehavior.Cascade);
	}
}

[Fact]
public void SharedEntityEntryTest()
{
	using SharedEntityContext context = new();
	
	TestEntity show = new();
	context.Entry(show); // This does not throw;

	TestShared id = new();
	context.Entry(id); // This throws.
}

Stack Traces

System.InvalidOperationException
The entity type 'TestShared' was not found. Ensure that the entity type has been added to the model.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetOrCreateEntry(Object entity)
   at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity)
   at Microsoft.EntityFrameworkCore.DbContext.Entry[TEntity](TEntity entity)
   at Kyoo.Tests.Database.GlobalTests.SharedEntityEntryTest() in /home/anonymus-raccoon/Projects/Kyoo/Kyoo/Kyoo.Tests/Database/SpecificTests/SanityTests.cs:line 82

Include provider and version information

EF Core version: 5.0.8
Database provider: Microsoft.EntityFrameworkCore.Sqlite 5.0.8 & Npgsql.EntityFrameworkCore.PostgreSQL 5.0.7
Target framework: NET 5.0
Operating system: Linux
IDE: Rider

@AndriySvyryd
Copy link
Member

Try context.Set<TestShared>("EntityShared").Attach(id)

@ajcvickers
Copy link
Member

Note for triage: Attach will always track the entity instance. We should consider an overload of "Entry" that takes the shared type name. This would return an EntityEntry without necessarily tracking it, such that things like shadow PKs can be set before starting to track.

@ajcvickers
Copy link
Member

Consider an Entry method on DbSet.

@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 Jul 14, 2022
ajcvickers added a commit that referenced this issue Jul 14, 2022
…instances

Fixes #25354

Also added some tests and fixed a couple of other issues with using shared-type entity type instances with tracking, etc.
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc1 Jul 22, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc1, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants