Skip to content

Commit 7e12fe5

Browse files
authoredMar 6, 2025
Merge pull request #1 from stevsharp/feature/efcore-loading
Feature/efcore loading
2 parents c208f52 + 2c75a9e commit 7e12fe5

File tree

10 files changed

+254
-229
lines changed

10 files changed

+254
-229
lines changed
 

‎.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@
88
/EfCoreGlobalFilter/obj
99
/.vs/EfCoreGlobalFilter/FileContentIndex
1010
/.vs/EfCoreGlobalFilter/DesignTimeBuild
11+
/ConsolefEfcoreloading/bin/Debug/net9.0
12+
/ConsolefEfcoreloading/obj/Debug/net9.0
13+
/ConsolefEfcoreloading/obj/ConsolefEfcoreloading.csproj.nuget.dgspec.json
14+
/ConsolefEfcoreloading/obj/ConsolefEfcoreloading.csproj.nuget.g.props
15+
/ConsolefEfcoreloading/obj/ConsolefEfcoreloading.csproj.nuget.g.targets
16+
/ConsolefEfcoreloading/obj/project.assets.json
17+
/.vs/EfCoreGlobalFilter/v17/DocumentLayout.backup.json
18+
/.vs/EfCoreGlobalFilter/v17/DocumentLayout.json

‎.vs/EfCoreGlobalFilter/v17/.suo

15 KB
Binary file not shown.

‎.vs/EfCoreGlobalFilter/v17/DocumentLayout.backup.json

-114
This file was deleted.

‎.vs/EfCoreGlobalFilter/v17/DocumentLayout.json

-114
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.2">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.2" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.2" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using ConsolefEfcoreloading.Model;
2+
3+
using Microsoft.EntityFrameworkCore;
4+
5+
public class AppDbContext : DbContext
6+
{
7+
public DbSet<Customer> Customers { get; set; }
8+
public DbSet<Address> Addresses { get; set; }
9+
public DbSet<Order> Orders { get; set; }
10+
11+
private readonly bool _useLazyLoading;
12+
13+
public AppDbContext(bool useLazyLoading = false)
14+
{
15+
_useLazyLoading = useLazyLoading;
16+
}
17+
18+
protected override void OnConfiguring(DbContextOptionsBuilder options)
19+
{
20+
var builder = options.UseSqlite("Data Source=mydb.db");
21+
22+
// Conditionally enable lazy loading
23+
if (_useLazyLoading)
24+
{
25+
builder.UseLazyLoadingProxies();
26+
}
27+
}
28+
protected override void OnModelCreating(ModelBuilder modelBuilder)
29+
{
30+
31+
modelBuilder.Entity<Customer>()
32+
.HasOne(c => c.Address)
33+
.WithOne(a => a.Customer)
34+
.HasForeignKey<Address>(a => a.CustomerId)
35+
.OnDelete(DeleteBehavior.Cascade);
36+
37+
modelBuilder.Entity<Customer>()
38+
.HasMany(c => c.Orders)
39+
.WithOne(o => o.Customer)
40+
.HasForeignKey(o => o.CustomerId);
41+
}
42+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace ConsolefEfcoreloading.Model;
2+
3+
public class Customer
4+
{
5+
public int Id { get; set; }
6+
public string? Name { get; set; }
7+
8+
// One-to-One relationship with Address
9+
public virtual Address Address { get; set; } = default!;
10+
11+
// One-to-Many relationship with Orders
12+
public virtual List<Order> Orders { get; set; } = new();
13+
}
14+
15+
public class Address
16+
{
17+
public int Id { get; set; }
18+
public string Street { get; set; } = default!;
19+
public string City { get; set; } = default!;
20+
public string Country { get; set; } = default!;
21+
22+
// Foreign Key to Customer
23+
public int CustomerId { get; set; }
24+
public virtual Customer Customer { get; set; } = default!;
25+
}
26+
27+
public class Order
28+
{
29+
public int Id { get; set; }
30+
public string? OrderNumber { get; set; }
31+
32+
// Foreign Key to Customer
33+
public int CustomerId { get; set; }
34+
public virtual Customer Customer { get; set; } = default!;
35+
}

‎ConsolefEfcoreloading/Program.cs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+

2+
3+
using Castle.Core.Resource;
4+
5+
using ConsolefEfcoreloading.Model;
6+
7+
using Microsoft.EntityFrameworkCore;
8+
9+
try
10+
{
11+
using (var context = new AppDbContext())
12+
{
13+
14+
context.Database.EnsureDeleted();
15+
context.Database.EnsureCreated();
16+
17+
var customer = new Customer
18+
{
19+
Name = "John Doe",
20+
Address = new Address
21+
{
22+
Street = "123 Main St",
23+
City = "New York",
24+
Country = "USA"
25+
},
26+
Orders = new List<Order>
27+
{
28+
new Order { OrderNumber = "ORD123" },
29+
new Order { OrderNumber = "ORD124" }
30+
}
31+
};
32+
33+
context.Customers.Add(customer);
34+
context.SaveChanges();
35+
}
36+
37+
38+
// Lazy-loaded automatically
39+
40+
Console.WriteLine($"Lazy-loaded automatically");
41+
42+
using (var context = new AppDbContext(true))
43+
{
44+
45+
var order = context.Orders.First();
46+
47+
var customer = order.Customer;
48+
49+
Console.WriteLine($"Customer Name: {customer.Name}");
50+
51+
}
52+
53+
Console.WriteLine($"Use Eager Loading");
54+
55+
// Use Eager Loading
56+
using (var context = new AppDbContext(false))
57+
{
58+
var order = context.Orders
59+
.Include(o => o.Customer)
60+
.First();
61+
62+
Console.WriteLine($"Customer Name: {order.Customer.Name}");
63+
}
64+
65+
// Use Explicit Loading
66+
Console.WriteLine($"Use Explicit Loading");
67+
using (var context = new AppDbContext(false))
68+
{
69+
var order = context.Orders.First();
70+
71+
context.Entry(order).Reference(o => o.Customer).Load();
72+
73+
var customer = order.Customer;
74+
75+
Console.WriteLine($"Customer Name: {customer.Name}");
76+
}
77+
78+
}
79+
catch (Exception ex)
80+
{
81+
Console.WriteLine(ex);
82+
}
83+
84+
Console.WriteLine("\nPress any key to exit...");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"version": 2,
3+
"dgSpecHash": "abzlcYzDxp4=",
4+
"success": true,
5+
"projectFilePath": "C:\\git\\EfCoreAndGlobalFilter\\ConsolefEfcoreloading\\ConsolefEfcoreloading.csproj",
6+
"expectedPackageFiles": [
7+
"C:\\Users\\Spyro\\.nuget\\packages\\castle.core\\5.1.1\\castle.core.5.1.1.nupkg.sha512",
8+
"C:\\Users\\Spyro\\.nuget\\packages\\humanizer.core\\2.14.1\\humanizer.core.2.14.1.nupkg.sha512",
9+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.bcl.asyncinterfaces\\7.0.0\\microsoft.bcl.asyncinterfaces.7.0.0.nupkg.sha512",
10+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.build.framework\\17.8.3\\microsoft.build.framework.17.8.3.nupkg.sha512",
11+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.build.locator\\1.7.8\\microsoft.build.locator.1.7.8.nupkg.sha512",
12+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.analyzers\\3.3.4\\microsoft.codeanalysis.analyzers.3.3.4.nupkg.sha512",
13+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.common\\4.8.0\\microsoft.codeanalysis.common.4.8.0.nupkg.sha512",
14+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.csharp\\4.8.0\\microsoft.codeanalysis.csharp.4.8.0.nupkg.sha512",
15+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.csharp.workspaces\\4.8.0\\microsoft.codeanalysis.csharp.workspaces.4.8.0.nupkg.sha512",
16+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.workspaces.common\\4.8.0\\microsoft.codeanalysis.workspaces.common.4.8.0.nupkg.sha512",
17+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.codeanalysis.workspaces.msbuild\\4.8.0\\microsoft.codeanalysis.workspaces.msbuild.4.8.0.nupkg.sha512",
18+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.data.sqlite.core\\9.0.2\\microsoft.data.sqlite.core.9.0.2.nupkg.sha512",
19+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore\\9.0.2\\microsoft.entityframeworkcore.9.0.2.nupkg.sha512",
20+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.abstractions\\9.0.2\\microsoft.entityframeworkcore.abstractions.9.0.2.nupkg.sha512",
21+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.analyzers\\9.0.2\\microsoft.entityframeworkcore.analyzers.9.0.2.nupkg.sha512",
22+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.design\\9.0.2\\microsoft.entityframeworkcore.design.9.0.2.nupkg.sha512",
23+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.proxies\\9.0.2\\microsoft.entityframeworkcore.proxies.9.0.2.nupkg.sha512",
24+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.relational\\9.0.2\\microsoft.entityframeworkcore.relational.9.0.2.nupkg.sha512",
25+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.sqlite\\9.0.2\\microsoft.entityframeworkcore.sqlite.9.0.2.nupkg.sha512",
26+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.entityframeworkcore.sqlite.core\\9.0.2\\microsoft.entityframeworkcore.sqlite.core.9.0.2.nupkg.sha512",
27+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.caching.abstractions\\9.0.2\\microsoft.extensions.caching.abstractions.9.0.2.nupkg.sha512",
28+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.caching.memory\\9.0.2\\microsoft.extensions.caching.memory.9.0.2.nupkg.sha512",
29+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.configuration.abstractions\\9.0.2\\microsoft.extensions.configuration.abstractions.9.0.2.nupkg.sha512",
30+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\9.0.2\\microsoft.extensions.dependencyinjection.9.0.2.nupkg.sha512",
31+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\9.0.2\\microsoft.extensions.dependencyinjection.abstractions.9.0.2.nupkg.sha512",
32+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.dependencymodel\\9.0.2\\microsoft.extensions.dependencymodel.9.0.2.nupkg.sha512",
33+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.logging\\9.0.2\\microsoft.extensions.logging.9.0.2.nupkg.sha512",
34+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.logging.abstractions\\9.0.2\\microsoft.extensions.logging.abstractions.9.0.2.nupkg.sha512",
35+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.options\\9.0.2\\microsoft.extensions.options.9.0.2.nupkg.sha512",
36+
"C:\\Users\\Spyro\\.nuget\\packages\\microsoft.extensions.primitives\\9.0.2\\microsoft.extensions.primitives.9.0.2.nupkg.sha512",
37+
"C:\\Users\\Spyro\\.nuget\\packages\\mono.texttemplating\\3.0.0\\mono.texttemplating.3.0.0.nupkg.sha512",
38+
"C:\\Users\\Spyro\\.nuget\\packages\\sqlitepclraw.bundle_e_sqlite3\\2.1.10\\sqlitepclraw.bundle_e_sqlite3.2.1.10.nupkg.sha512",
39+
"C:\\Users\\Spyro\\.nuget\\packages\\sqlitepclraw.core\\2.1.10\\sqlitepclraw.core.2.1.10.nupkg.sha512",
40+
"C:\\Users\\Spyro\\.nuget\\packages\\sqlitepclraw.lib.e_sqlite3\\2.1.10\\sqlitepclraw.lib.e_sqlite3.2.1.10.nupkg.sha512",
41+
"C:\\Users\\Spyro\\.nuget\\packages\\sqlitepclraw.provider.e_sqlite3\\2.1.10\\sqlitepclraw.provider.e_sqlite3.2.1.10.nupkg.sha512",
42+
"C:\\Users\\Spyro\\.nuget\\packages\\system.codedom\\6.0.0\\system.codedom.6.0.0.nupkg.sha512",
43+
"C:\\Users\\Spyro\\.nuget\\packages\\system.collections.immutable\\7.0.0\\system.collections.immutable.7.0.0.nupkg.sha512",
44+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition\\7.0.0\\system.composition.7.0.0.nupkg.sha512",
45+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition.attributedmodel\\7.0.0\\system.composition.attributedmodel.7.0.0.nupkg.sha512",
46+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition.convention\\7.0.0\\system.composition.convention.7.0.0.nupkg.sha512",
47+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition.hosting\\7.0.0\\system.composition.hosting.7.0.0.nupkg.sha512",
48+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition.runtime\\7.0.0\\system.composition.runtime.7.0.0.nupkg.sha512",
49+
"C:\\Users\\Spyro\\.nuget\\packages\\system.composition.typedparts\\7.0.0\\system.composition.typedparts.7.0.0.nupkg.sha512",
50+
"C:\\Users\\Spyro\\.nuget\\packages\\system.diagnostics.eventlog\\6.0.0\\system.diagnostics.eventlog.6.0.0.nupkg.sha512",
51+
"C:\\Users\\Spyro\\.nuget\\packages\\system.io.pipelines\\7.0.0\\system.io.pipelines.7.0.0.nupkg.sha512",
52+
"C:\\Users\\Spyro\\.nuget\\packages\\system.memory\\4.5.3\\system.memory.4.5.3.nupkg.sha512",
53+
"C:\\Users\\Spyro\\.nuget\\packages\\system.reflection.metadata\\7.0.0\\system.reflection.metadata.7.0.0.nupkg.sha512",
54+
"C:\\Users\\Spyro\\.nuget\\packages\\system.runtime.compilerservices.unsafe\\6.0.0\\system.runtime.compilerservices.unsafe.6.0.0.nupkg.sha512",
55+
"C:\\Users\\Spyro\\.nuget\\packages\\system.text.json\\9.0.2\\system.text.json.9.0.2.nupkg.sha512",
56+
"C:\\Users\\Spyro\\.nuget\\packages\\system.threading.channels\\7.0.0\\system.threading.channels.7.0.0.nupkg.sha512"
57+
],
58+
"logs": []
59+
}

‎EfCoreGlobalFilter.sln

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.13.35818.85 d17.13
4+
VisualStudioVersion = 17.13.35818.85
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EfCoreGlobalFilter", "EfCoreGlobalFilter\EfCoreGlobalFilter.csproj", "{1943DF80-6398-40CC-9221-6B1725EF39AC}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsolefEfcoreloading", "ConsolefEfcoreloading\ConsolefEfcoreloading.csproj", "{DF33E9C7-706C-461A-AE39-1D82C99A368E}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{1943DF80-6398-40CC-9221-6B1725EF39AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{1943DF80-6398-40CC-9221-6B1725EF39AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{1943DF80-6398-40CC-9221-6B1725EF39AC}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{DF33E9C7-706C-461A-AE39-1D82C99A368E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{DF33E9C7-706C-461A-AE39-1D82C99A368E}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{DF33E9C7-706C-461A-AE39-1D82C99A368E}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{DF33E9C7-706C-461A-AE39-1D82C99A368E}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)
Please sign in to comment.