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

Seeding data with multiple items causes stack overflow #4

Open
punkouter2021 opened this issue Oct 28, 2019 · 2 comments
Open

Seeding data with multiple items causes stack overflow #4

punkouter2021 opened this issue Oct 28, 2019 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@punkouter2021
Copy link

Using the pattern here I am trying to seed an ibject that contains another collection... Details here.. Any help is grat.. Meanwhile Ill try removing all the EntityBase stuff to see if thats the problem.

https://stackoverflow.com/questions/58595744/why-does-adding-more-than-1-item-to-this-collection-in-efcore-cause-a-stack-over

@mehmetozkaya mehmetozkaya self-assigned this Oct 30, 2019
@mehmetozkaya mehmetozkaya added the bug Something isn't working label Oct 30, 2019
@mehmetozkaya
Copy link
Member

Thank you for your report. Let me see what is the error and how we can manage it. During this time if you solved that could you please send me your pull request over github ?

@Ungerfall
Copy link

The issue is that there is a recursive infinite call

if (item.IsTransient() || IsTransient())
    return false;
else
    return item == this;

which calls

public static bool operator ==(EntityBase<TId> left, EntityBase<TId> right)
{
    if (Equals(left, null))
        return Equals(right, null) ? true : false;
    else
        return left.Equals(right);
}

The correct implementation would be

public override bool Equals(object? obj)
{
    if (obj is null)
    {
        return false;
    }

    if (ReferenceEquals(this, obj))
    {
        return true;
    }

    if (GetType() != obj.GetType())
    {
        return false;
    }

    if (obj is not EntityBase<TId> entity)
    {
        return false;
    }

    return Id.Equals(entity.Id); // requires IEquatable<TId> generic constraint
}
public static bool operator ==(EntityBase<TId> left, EntityBase<TId> right)
{
    return left is not null && right is not null && left.Equals(right);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants