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

Owned entity not loaded if PK has a value converter #26116

Closed
AndriySvyryd opened this issue Sep 20, 2021 · 0 comments · Fixed by #26240
Closed

Owned entity not loaded if PK has a value converter #26116

AndriySvyryd opened this issue Sep 20, 2021 · 0 comments · Fixed by #26240
Labels
area-change-tracking area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@AndriySvyryd
Copy link
Member

[ConditionalFact]
public void Can_query_and_update_owned_entity_with_value_converter()
{
    using var testDatabase = SqlServerTestStore.CreateInitialized(DatabaseName);            

    var options = Fixture.CreateOptions(testDatabase);
    using (var context = new ValueConvertedOwnedContext(options))
    {
        context.Database.EnsureCreatedResiliently();

        var key = new Key("1-1-1");
        var text = new TextEntity() { Position = 1 };

        var ownedEntity = new BaseEntity(key, text);

        context.OwnedEntityTest.Add(ownedEntity);

        context.SaveChanges();
    }

    using (var context = new ValueConvertedOwnedContext(options))
    {
        var key = new Key("1-1-1");
        var ownedEntity = context.OwnedEntityTest.Single(o => o.Name == key);

        Assert.Equal(1, ownedEntity.Text.Position);

        var updatedText = new TextEntity() { Position = 0 };

        ownedEntity.Text = updatedText;

        context.Set<BaseEntity>().Update(ownedEntity);

        context.SaveChanges();
    }

    using (var context = new ValueConvertedOwnedContext(options))
    {
        var key = new Key("1-1-1");
        var ownedEntity = context.OwnedEntityTest.Find(key);

        Assert.Equal(0, ownedEntity.Text.Position);
    }
}

public class Key
{
    public string Value { get; }

    public Key(string id)
    {
        Value = id;
    }
}

public class BaseEntity
{
    public BaseEntity()
    {
    }

    public BaseEntity(Key key, TextEntity text)
    {
        Name = key;
        Text = text;
    }

    public Key Name { get; set; }
    public TextEntity Text { get; set; }
}

public class TextEntity
{
    public int Position { get; set; }
}

private class ValueConvertedOwnedContext : DbContext
{
    public ValueConvertedOwnedContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet<BaseEntity> OwnedEntityTest { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BaseEntity>(entity =>
        {
            entity.ToTable("OwnedEntityTest");

            entity.HasKey(e => e.Name);

            entity.Property(p => p.Name)
                .HasConversion(p => p.Value, p => new Key(p));

            entity.OwnsOne(p => p.Text, p =>
            {
                entity.ToTable("OwnedEntityTest");
            });

            entity.Navigation(p => p.Text).IsRequired();
        });
    }
}
@ajcvickers ajcvickers self-assigned this Sep 24, 2021
@ajcvickers ajcvickers added this to the 6.0.0 milestone Sep 24, 2021
ajcvickers added a commit that referenced this issue Oct 3, 2021
Fixes #26116

Initial issue was that the converted type also required a comparer. On adding one it revealed that in this case a default comparer was being used for the dependent map in the state manager. Fix is to use the PK comparer in the same was as we use the PK converter.

Added more testing for similar cases including generated shadow FKs.
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. area-save-changes and removed area-query labels Oct 3, 2021
@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
area-change-tracking area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants