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

It has an endless loop, and its ideal result is the same as "Teacher=null", because I have no Include. #29364

Closed
panjonsen opened this issue Oct 15, 2022 · 10 comments

Comments

@panjonsen
Copy link

It has an endless loop, and its ideal result is the same as "Teacher=null", because I have no Include.

image
image
image
image

    public class Student
    {
        public long StudentId { get; set; }

        public string StudentName { get; set; }

        public List<Teacher> Teachers { get; set; } = new List<Teacher>();

        public List<Student_Teacher> Student_Teachers { get; set; } = new List<Student_Teacher>();
    }
    public class Teacher
    {
        public long TeacherId { get; set; }

        public string TeacherName { get; set; }

        public List<Student> Students { get; set; } = new List<Student>();

        public List<Student_Teacher> Student_Teachers { get; set; } = new List<Student_Teacher>();
    }
    public class Student_Teacher
    {
        public long StudentId { get; set; }

        public long TeacherId { get; set; }

        public Student? Student { get; set; } 

        public Teacher? Teacher { get; set; }

        public int IsBind { get; set; }
    }
            var list = db.Students
                 .AsNoTracking()
                   .Include(m => m.Student_Teachers)
                   .ToList();

            Console.WriteLine(list);

Include provider and version information

EF Core version:6.0.9
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework:NET 6.0
Operating system:
IDE: 2022

@panjonsen
Copy link
Author

My expression may not be very good. The problem I want to say is that it is infinitely nested.

@panjonsen
Copy link
Author

I don't know how to solve this problem. I hope I can get help. Thank you.

@panjonsen
Copy link
Author

它有一个无限循环,它的理想结果与“Teacher=null”相同,因为我没有Include。

图片 图片 图片 图片

    public class Student
    {
        public long StudentId { get; set; }

        public string StudentName { get; set; }

        public List<Teacher> Teachers { get; set; } = new List<Teacher>();

        public List<Student_Teacher> Student_Teachers { get; set; } = new List<Student_Teacher>();
    }
    public class Teacher
    {
        public long TeacherId { get; set; }

        public string TeacherName { get; set; }

        public List<Student> Students { get; set; } = new List<Student>();

        public List<Student_Teacher> Student_Teachers { get; set; } = new List<Student_Teacher>();
    }
    public class Student_Teacher
    {
        public long StudentId { get; set; }

        public long TeacherId { get; set; }

        public Student? Student { get; set; } 

        public Teacher? Teacher { get; set; }

        public int IsBind { get; set; }
    }
            var list = db.Students
                 .AsNoTracking()
                   .Include(m => m.Student_Teachers)
                   .ToList();

            Console.WriteLine(list);

包括提供者和版本信息

EF Core version:6.0.9 Database provider: Pomelo.EntityFrameworkCore.MySql Target framework:NET 6.0 Operating system: IDE: 2022

    internal class Student_TeacherConfig : IEntityTypeConfiguration<Student_Teacher>
    {
        public void Configure(EntityTypeBuilder<Student_Teacher> builder)
        {
            builder.ToTable("T_Student_Teacher");

            ////复合主键
            builder.HasKey(b => new { b.StudentId, b.TeacherId });

            builder.Property(m => m.IsBind).HasDefaultValue(0);
        }
    }
   internal class StudentConfig : IEntityTypeConfiguration<Student>
    {
        public void Configure(EntityTypeBuilder<Student> builder)
        {
            builder.ToTable("T_Student");

            builder.HasMany(m => m.Student_Teachers)
                .WithOne(m => m.Student)
                .HasForeignKey(m => m.StudentId);
        }
    }
    internal class TeacherConfig : IEntityTypeConfiguration<Teacher>
    {
        public void Configure(EntityTypeBuilder<Teacher> builder)
        {
            builder.ToTable("T_Teacher");

            builder.HasMany(m => m.Student_Teachers)
               .WithOne(m => m.Teacher)
               .HasForeignKey(m => m.TeacherId);
        }
    }

@cschuchardt88
Copy link

I have something similar to this. Seems to be a problem with include function

@panjonsen
Copy link
Author

I have something similar to this. Seems to be a problem with include function

In my opinion, many historical questions are similar to this problem. It seems that there is no good solution. There is self reference. If the referenced object is still its own object, the result will be extended indefinitely.

@ajcvickers
Copy link
Member

@panjonsen See #11564. Make sure to read the full discussion to really understand what is going on.

@panjonsen
Copy link
Author

@ajcvickers I have seen this problem, but it seems that there is no effective solution. For example, you can set it not to be fixed by infinite loops through features or code. As a result, people need to do a lot of extra things to make up for him. At present, I also use select to repair, but I think this is just a forced method. It should not be the solution he really should have.

@panjonsen
Copy link
Author

@ajcvickers I hope that the solution proposed with previous developers will be more effective and useful. To set it so that it will not continue to be repaired.

@ajcvickers
Copy link
Member

@panjonsen Your questions are answered in the comments on #11564.

@panjonsen
Copy link
Author

services.AddDbContext(options =>
{
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});

This is the best solution I think, but it will also bring some additional work, such as the update needs additional processing.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants