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

ConsecutiveIndex constraint fail when using multiple Apply methods on save entity #72

Closed
vermilion opened this issue May 19, 2022 · 2 comments · Fixed by #73
Closed

Comments

@vermilion
Copy link

Current behavior:
When using multiple Apply methods EFCore provider doesn't preserve their order on insert.

My code that fails

        Apply(new CompetitionCreatedEvent());
        Apply(new CompetitionNameSetEvent(name));
        Apply(new CompetitionLogoSetEvent(logo));

EF then try to sort entities alphabetically on insert thus getting into constraint violation

You can check issue here dotnet/efcore#11686
This issue lead me to PR dotnet/efcore#25874 where inserted entities are ordered using StringComparer.Ordinal

After digging source code found the piece of code that cause this

	case AddEventsAction(var events):
    	_store.Context.AddRange(events);
        break;

Proposed solution:
As a solution I can only think of Bulk insert wrapped in transaction. Maybe you'll find a better way

@BramKraai BramKraai linked a pull request May 19, 2022 that will close this issue
@BramKraai
Copy link
Contributor

Hey,

Thanks for your very well written issue!

We were able to reproduce the issue in our tests and saw that EF Core indeed does not preserve insertion order, leading to a violated ConsecutiveIndex constraint while using Postgres. For reasons unknown to me, the same error does not occur in SQL Server.

I've linked a proposed solution PR, but we might investigate alternative solutions before merging the PR.

@BramKraai
Copy link
Contributor

This issue has been fixed in version 0.3.1

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

Successfully merging a pull request may close this issue.

2 participants