EvAlex / ef-db-diagrams

Visualize model created with EntityFramework Core
https://db-diagrams.firebaseapp.com/
MIT License
62 stars 26 forks source link

StackOverflow with self-referencing entities #45

Open mpartipilo opened 6 years ago

mpartipilo commented 6 years ago

An entity like this will cause a StackOverflow.

    public class Project
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public Guid? ParentId { get; set; }
        public Project Parent { get; set; }

        public ICollection<Project> Children { get; set; }
}
mpartipilo commented 6 years ago

I may have found the problem: Namespace is compared to other.Name.

        public bool Equals(ClrType other)
        {
            return other is ClrType
                && Name == other.Name
                && Namespace == other.Name
                && Assembly == other.Assembly
                && GenericTypeArguments.Count() == other.GenericTypeArguments.Count()
                && GenericTypeArguments.All(e => other.GenericTypeArguments.Any(ee => e.Equals(ee)));
        }

        public bool Equals(Type other)
        {
            return other is Type
                && Name == other.Name
                && Namespace == other.Name
                && Assembly == other.AssemblyQualifiedName
                && GenericTypeArguments.Count() == other.GenericTypeArguments.Length
                && GenericTypeArguments.All(e => other.GenericTypeArguments.Any(ee => e.Equals(ee)));
        }

Now, next challenge: Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'principalEntity' with type 'EntityFrameworkCore.Diagrams.Dto.DbEntity'. Path 'entities[0].foreignKeys[0]'.'

mpartipilo commented 6 years ago

I think I will just refactor my entities to get rid of the self reference via an external entity. I might do a PR for the .Equals fix, but feel free to do it yourself if I don't make it in time.