You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inspired by and partially based on the blog post: https://weblogs.asp.net/ricardoperes/implementing-missing-features-in-entity-framework-core-part-6-lazy-loading
5
5
6
6
# How to enable LazyLoading in EF Core?
7
7
8
-
1. Reference the `Microsoft.EntityFrameworkCore.LazyLoading` NuGet package (https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.LazyLoading/).
9
-
2. Create (or modify an existing) DbContext factory. Include the lines inside the two `if(_isLazy)` blocks in your DbContext factory (3 lines total - 2 before building the DbContext, and 1 after):
8
+
Enabling LazyLoading in EF Core is extremely easy with this library. You just need to call `UseLazyLoading()` (see step 2 below).
9
+
10
+
However, you will need to slightly modify your entity classes, but just the References, not the Collections (see step 3 below).
11
+
12
+
## Step 1 - Nuget package
13
+
Reference the `Microsoft.EntityFrameworkCore.LazyLoading` NuGet package (https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.LazyLoading/).
14
+
## Step 2 - Configure the DbContext builder
15
+
Call `UseLazyLoading()` on the `DbContextOptionsBuilder` when creating the `DbContext`.
3. In your model you need to declare References using the type LazyReference<T>. Collections don't require additional configuration in your model, just use the ICollection<> type.
38
+
## Step 3 - Adjust the model
39
+
In your model you need to declare References using the type `LazyReference<T>`. Collections don't require additional configuration in your model, just use the `ICollection<>` type.
49
40
```c#
50
41
publicclassParent
51
42
{
@@ -68,4 +59,5 @@ public class Child
68
59
}
69
60
}
70
61
```
71
-
4. That's all, LazyLoading enabled.
62
+
## Step 4 - Done!
63
+
That's all, LazyLoading enabled! It was so easy, wasn't it?
thrownewLazyLoadingConfigurationException($"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
42
+
thrownewLazyLoadingConfigurationException(
43
+
$"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
thrownewLazyLoadingConfigurationException($"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
44
+
thrownewLazyLoadingConfigurationException(
45
+
$"Service of type '{typeof(EntityFrameworkCore.Internal.IConcurrencyDetector).FullName}' must be replaced by a service of type '{typeof(IConcurrencyDetector).FullName}' in order to use LazyLoading");
0 commit comments