Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FilteringResultsInsideInclude.Migrations
{
/// <inheritdoc />
public partial class AddAssignment : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Assignments",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
StudentId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Assignments", x => x.Id);
table.ForeignKey(
name: "FK_Assignments_Students_StudentId",
column: x => x.StudentId,
principalTable: "Students",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Assignments_StudentId",
table: "Assignments",
column: "StudentId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Assignments");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,39 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.8")
.HasAnnotation("ProductVersion", "10.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("FilteringResultsInsideInclude.Models.Assignment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<int>("StudentId")
.HasColumnType("int");

b.Property<string>("Title")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.HasIndex("StudentId");

b.ToTable("Assignments");
});

modelBuilder.Entity("FilteringResultsInsideInclude.Models.Course", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Title")
.HasColumnType("nvarchar(max)");
Expand All @@ -43,7 +64,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<int>("CourseId")
.HasColumnType("int");
Expand All @@ -61,6 +82,17 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("Students");
});

modelBuilder.Entity("FilteringResultsInsideInclude.Models.Assignment", b =>
{
b.HasOne("FilteringResultsInsideInclude.Models.Student", "Student")
.WithMany("Assignments")
.HasForeignKey("StudentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Student");
});

modelBuilder.Entity("FilteringResultsInsideInclude.Models.Student", b =>
{
b.HasOne("FilteringResultsInsideInclude.Models.Course", "Course")
Expand All @@ -76,6 +108,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
b.Navigation("Students");
});

modelBuilder.Entity("FilteringResultsInsideInclude.Models.Student", b =>
{
b.Navigation("Assignments");
});
#pragma warning restore 612, 618
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ public class AppDbContext : DbContext
{
public DbSet<Student>? Students { get; set; }
public DbSet<Course>? Courses { get; set; }
public DbSet<Assignment>? Assignments { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.;Database=AppDB;Trusted_Connection=True;");
optionsBuilder.UseSqlServer(@"Server=.;Database=AppDB;Trusted_Connection=True;TrustServerCertificate=True;");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace FilteringResultsInsideInclude.Models
{
public class Assignment
{
public int Id { get; set; }
public string? Title { get; set; }

public int StudentId { get; set; }
public Student? Student { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public class Student

public int CourseId { get; set; }
public Course? Course { get; set; }

public ICollection<Assignment>? Assignments { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class Queries
{
public const int CourseCount = 10;
public const int StudentCountPerCourse = 100;

public const int AssignmentCountPerStudent = 2;

public static async Task SeedData(AppDbContext context)
{
if (!context.Courses!.Any())
Expand All @@ -18,7 +19,11 @@ public static async Task SeedData(AppDbContext context)
Students = Enumerable.Range(1, StudentCountPerCourse).Select(j => new Student()
{
Name = $"Student {10 * i + j}",
Mark = j
Mark = j,
Assignments = Enumerable.Range(1, AssignmentCountPerStudent).Select(k => new Assignment
{
Title = $"Assignment {k}"
}).ToList()
}).ToList()
}).ToList();

Expand Down Expand Up @@ -55,6 +60,18 @@ public static List<Course> GoodFilteringOnMultipleInclude(AppDbContext context)
return goodQuery;
}

public static List<Course> GoodFilteringOnMultipleIncludeWithThenInclude(AppDbContext context)
{
var goodQuery = context.Courses!
.Include(c => c.Students!.Where(s => s.Mark > 50))
.ThenInclude(s => s.Assignments)
.Include(c => c.Students!)
.ThenInclude(s => s.Assignments)
.ToList();

return goodQuery;
}

public static List<Course> BadFilteringOnMultipleInclude(AppDbContext context)
{
var badQuery = context.Courses!
Expand Down Expand Up @@ -98,7 +115,7 @@ public static List<Course> FilteredIncludeWithTrackingQueries2(AppDbContext cont
public static List<Course> FilteringInsideIncludeAndSelectMethod(AppDbContext context)
{
var courses = context.Courses!
.Include(x => x.Students!.Where(x => x.Mark > 50))
.Include(c => c.Students!.Where(s => s.Mark > 50))
.Select(c => new Course
{
Id = c.Id,
Expand All @@ -122,5 +139,37 @@ public static List<Course> FilteringInsideSelectMethodWithoutInclude(AppDbContex
return courses;
}

public static List<Course> FilteredIncludeWithSortingAndPaging(AppDbContext context)
{
var topStudents = context.Courses!
.Include(c => c.Students!
.Where(s => s.Mark > 50)
.OrderByDescending(s => s.Mark)
.Take(3))
.ToList();

return topStudents;
}

public static List<Course> FilteredIncludeWithoutMatchingStudents(AppDbContext context)
{
var courses = context.Courses!
.Include(c => c.Students!.Where(s => s.Mark > StudentCountPerCourse))
.ToList();

return courses;
}

public static List<Student> ExplicitLoadingWithFilter(AppDbContext context, Course course)
{
var students = context.Entry(course)
.Collection(c => c.Students!)
.Query()
.Where(s => s.Mark > 50)
.ToList();

return students;
}

}
}
Loading
Loading