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

IdentityMap throws when records are loaded & modified. #2471

Closed
Hawxy opened this issue Jan 24, 2023 · 2 comments · Fixed by #2473
Closed

IdentityMap throws when records are loaded & modified. #2471

Hawxy opened this issue Jan 24, 2023 · 2 comments · Fixed by #2473

Comments

@Hawxy
Copy link
Contributor

Hawxy commented Jan 24, 2023

When using an identity map, it will throw when records that have been previously loaded are modified & stored, as the record will not meet the reference equality check.

       ProjectAsync<MyEntityEdited>(async (edited, operations) =>
        {
            var entities = await operations.LoadManyAsync<MyEntity>(created.IdList);

            var updatedEntities = entities.Select(x => x with { SomeId = edited.Id }).ToArray();
            operations.Store(updatedEntities);
        });
System.InvalidOperationException
Document 'MyEntity' with same Id already added to the session.
   at Marten.Internal.Storage.IdentityMapDocumentStorage`2.store(IMartenSession session, T document, TId& id)
   at Marten.Internal.Storage.IdentityMapDocumentStorage`2.Store(IMartenSession session, T document)
   at Marten.Internal.Sessions.DocumentSessionBase.storeEntity[T](T entity, IDocumentStorage`1 storage)
   at Marten.Internal.Sessions.DocumentSessionBase.store[T](IEnumerable`1 entities)
   at Marten.Internal.Sessions.DocumentSessionBase.Store[T](T[] entities)

Relevant part of the identity map:

if (!ReferenceEquals(existing, document))

@Hawxy Hawxy changed the title EventProjection uses IdentityMap by default EventProjection's DocumentOperations IdentityMap throws on document store Jan 24, 2023
Hawxy added a commit to Hawxy/marten that referenced this issue Jan 26, 2023
@Hawxy Hawxy changed the title EventProjection's DocumentOperations IdentityMap throws on document store IdentityMap throws when records are loaded & modified. Jan 26, 2023
@Hawxy
Copy link
Contributor Author

Hawxy commented Jan 31, 2023

@jeremydmiller I've fixed this locally by checking if the type implements IEquatable<T> and skipping the equality check if it does. Can push it up if you're fine with that as a solution.

@jeremydmiller
Copy link
Member

I'm good with that, go for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants