Skip to content

Commit 559134e

Browse files
First Commit
1 parent 55e8991 commit 559134e

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
obj/
2+
bin/
3+
.idea/

LazyLoader.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LazyLoader", "LazyLoader\LazyLoader.csproj", "{A49EF347-A6B4-4B78-A97B-DCAFE706BB90}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{A49EF347-A6B4-4B78-A97B-DCAFE706BB90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{A49EF347-A6B4-4B78-A97B-DCAFE706BB90}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{A49EF347-A6B4-4B78-A97B-DCAFE706BB90}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{A49EF347-A6B4-4B78-A97B-DCAFE706BB90}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

LazyLoader/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
obj/
3+
.idea/

LazyLoader/Context.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
#nullable disable
5+
6+
namespace LazyLoader
7+
{
8+
public partial class Context : DbContext
9+
{
10+
public Context()
11+
{
12+
}
13+
14+
public Context(DbContextOptions<Context> options)
15+
: base(options)
16+
{
17+
}
18+
19+
public virtual DbSet<Email> Emails { get; set; }
20+
public virtual DbSet<ParticipantList> ParticipantLists { get; set; }
21+
22+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
23+
{
24+
if (!optionsBuilder.IsConfigured)
25+
{
26+
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
27+
optionsBuilder
28+
.UseLazyLoadingProxies()
29+
.LogTo(Console.WriteLine)
30+
.EnableSensitiveDataLogging(true);
31+
32+
base.OnConfiguring(optionsBuilder);
33+
}
34+
}
35+
36+
protected override void OnModelCreating(ModelBuilder modelBuilder)
37+
{
38+
modelBuilder.Entity<Email>(entity =>
39+
{
40+
entity.HasOne(d => d.ParticipantList)
41+
.WithMany(p => p.Emails)
42+
.HasForeignKey(d => d.ParticipantListId)
43+
.OnDelete(DeleteBehavior.ClientSetNull);
44+
});
45+
OnModelCreatingPartial(modelBuilder);
46+
}
47+
48+
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
49+
}
50+
}

LazyLoader/Email.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
#nullable disable
8+
9+
namespace LazyLoader
10+
{
11+
[Table("emails")]
12+
public partial class Email
13+
{
14+
[Key]
15+
[Column("id", TypeName = "integer")]
16+
public long Id { get; set; }
17+
[Column("participant_list_id", TypeName = "integer")]
18+
public long? ParticipantListId { get; set; }
19+
[Required]
20+
[Column("email_address")]
21+
public string EmailAddress { get; set; }
22+
23+
[Required]
24+
[ForeignKey(nameof(ParticipantListId))]
25+
[InverseProperty("Emails")]
26+
public virtual ParticipantList ParticipantList { get; set; }
27+
}
28+
}

LazyLoader/LazyLoader.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.4.21253.1" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0-preview.4.21253.1" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-preview.4.21253.1" />
12+
</ItemGroup>
13+
14+
</Project>

LazyLoader/ParticipantList.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
#nullable disable
8+
9+
namespace LazyLoader
10+
{
11+
[Table("participant_lists")]
12+
[Index(nameof(Name), IsUnique = true)]
13+
public partial class ParticipantList
14+
{
15+
public ParticipantList()
16+
{
17+
Emails = new HashSet<Email>();
18+
}
19+
20+
[Key]
21+
[Column("id")]
22+
public long Id { get; set; }
23+
[Column("name")]
24+
public string Name { get; set; }
25+
26+
[InverseProperty(nameof(Email.ParticipantList))]
27+
public virtual ICollection<Email> Emails { get ; set; }
28+
}
29+
}

LazyLoader/Program.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace LazyLoader
7+
{
8+
class Program
9+
{
10+
private static readonly string dataFilename = "LazyLoader.db";
11+
12+
static private void DumpEntities(ParticipantList plist)
13+
{
14+
Console.WriteLine($"-------\nParticipantList '{plist.Name}' Id={plist.Id}");
15+
foreach (var email in plist.Emails)
16+
{
17+
Console.WriteLine($"\t{email.Id}: {email.EmailAddress}");
18+
}
19+
Console.WriteLine("-------\n");
20+
}
21+
22+
static void Main(string[] args)
23+
{
24+
Console.WriteLine("Why doesn't lazy loading work?");
25+
26+
System.IO.File.Delete(dataFilename);
27+
var options = new DbContextOptionsBuilder<Context>()
28+
.UseSqlite($"Data Source={dataFilename};")
29+
.Options;
30+
var db = new Context(options);
31+
db.Database.EnsureCreated();
32+
33+
var plist = new ParticipantList()
34+
{
35+
Name = "I'm a P List"
36+
};
37+
var email1 = new Email()
38+
{
39+
EmailAddress = "[email protected]",
40+
ParticipantList = plist,
41+
};
42+
var email2 = new Email()
43+
{
44+
EmailAddress = "[email protected]",
45+
ParticipantList = plist,
46+
};
47+
db.ParticipantLists.Add(plist);
48+
db.Emails.Add(email1);
49+
db.Emails.Add(email2);
50+
db.SaveChanges();
51+
52+
Console.WriteLine($"Before Clearing LazyLoadingEnabled={db.ChangeTracker.LazyLoadingEnabled}:");
53+
DumpEntities(plist);
54+
55+
Console.WriteLine($"Clearing Change Tracker");
56+
Console.WriteLine($"via LazyLoading LazyLoadingEnabled={db.ChangeTracker.LazyLoadingEnabled}");
57+
db.ChangeTracker.Clear();
58+
var persisted_plist = db.ParticipantLists.Find(plist.Id);
59+
DumpEntities(persisted_plist);
60+
61+
var emails = db.Emails.Where(u => u.ParticipantListId == plist.Id);
62+
Console.WriteLine($"After Also selecting emails from DB LazyLoadingEnabled={db.ChangeTracker.LazyLoadingEnabled}");
63+
DumpEntities(persisted_plist);
64+
65+
Console.WriteLine($"Clearing Change Tracker");
66+
Console.WriteLine($"via Include LazyLoadingEnabled={db.ChangeTracker.LazyLoadingEnabled}");
67+
db.ChangeTracker.Clear();
68+
persisted_plist = db.ParticipantLists.Where(u => u.Id == plist.Id).Include(pl => pl.Emails).First();
69+
DumpEntities(persisted_plist);
70+
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)