Behavior when you include the same field twice in the linq entity infrastructure

what happens when I turn on the same field twice, I consciously take an object with db, and I use the EF .include parameter. I mean the following:
I have:

 .Include(x => x.Student) .Include(x => x.Car) .Include(x => x.Student) 

This is the model:

 Person has a Student Person has a car 

therefore, having included (by mistake) a student twice (since my person is ONLY a student), is there a problem?
Postscript I just want it to include ONE! since I have only one student. Complains about it? I tried it, and everything was fine, but I do not know the consequences of this. Can someone explain / develop? Searched a little, but could not identify any problems.

+5
source share
3 answers

Take this sample:

 public class RentContext : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Rent> Rents { get; set; } public DbSet<Car> Cars { get; set; } } public class Car { public int Id { get; set; } public string Model { get; set; } public double Price { get; set; } } public class Rent { public int Id { get; set; } public Student Student { get; set; } public Car Car { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Year { get; set; } } 

The rent includes Student and Car.

Let's make a request with unique Include clauses:

 var rents = ctx.Rents .Include(x => x.Student) .Include(x => x.Car) //.Include(x => x.Student) .ToList(); 

This is the generated sql:

 SELECT [Extent1].[Id] AS [Id], [Extent2].[Id] AS [Id1], [Extent2].[Name] AS [Name], [Extent2].[Year] AS [Year], [Extent3].[Id] AS [Id2], [Extent3].[Model] AS [Model], [Extent3].[Price] AS [Price] FROM [dbo].[Rents] AS [Extent1] LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id] 

Make a duplicate query Include:

 var rents = ctx.Rents .Include(x => x.Student) .Include(x => x.Car) .Include(x => x.Student) .ToList(); 

You will get this sql:

 SELECT [Extent1].[Id] AS [Id], [Extent2].[Id] AS [Id1], [Extent2].[Name] AS [Name], [Extent2].[Year] AS [Year], [Extent3].[Id] AS [Id2], [Extent3].[Model] AS [Model], [Extent3].[Price] AS [Price] FROM [dbo].[Rents] AS [Extent1] LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id] 

As you can see, EF is smart enough to create the same sql even if you specify Include more than once.

UPDATE: Re-enable (many times)

Let's try this:

 var rents = ctx.Rents .Include(x => x.Student) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Car) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .Include(x => x.Student) .ToList(); 

Repeats Includes several times. And here is the generated sql:

 SELECT [Extent1].[Id] AS [Id], [Extent2].[Id] AS [Id1], [Extent2].[Name] AS [Name], [Extent2].[Year] AS [Year], [Extent3].[Id] AS [Id2], [Extent3].[Model] AS [Model], [Extent3].[Price] AS [Price] FROM [dbo].[Rents] AS [Extent1] LEFT OUTER JOIN [dbo].[Students] AS [Extent2] ON [Extent1].[Student_Id] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[Cars] AS [Extent3] ON [Extent1].[Car_Id] = [Extent3].[Id] 

Only the same code again. So yes. We could say that everything will be fine, although a little strange.

Hope this helps!

+4
source

If you call it twice, the same request will be created. The result is the same, something like this

 SELECT [Extent1].[Id] AS [Id], [Extent1].[Title] AS [Title], [Extent1].[PersonId] AS [PersonId], [Extent2].[Id] AS [Id1], [Extent2].[Name] AS [Name] FROM [dbo].[Books] AS [Extent1] INNER JOIN [dbo].[People] AS [Extent2] ON [Extent1].[PersonId] = [Extent2].[Id] 
+2
source

This is perfectly true and will not disable EF anyway. See how you include sitelinks and collections that diverge from a link, such as Student . You can write them like this. What you suggested will not confuse EF more than in the following example.

 .Include(x => x.Student.Teacher) .Include(x => x.Car) .Include(x => x.Student.DrivingLog) 
+1
source

Source: https://habr.com/ru/post/1258839/


All Articles