Automatically update persisted computed properties in EF Core—so you don’t have to!
Denormalizing data is a powerful performance optimization, but manually managing computed properties is error-prone and time-consuming. With EF Core Auto Compute:
- 🚀 Boost Performance
Eliminate costly joins and runtime calculations by persisting derived data. - 🔄 Stay Consistent
Automatically keep computed values accurate as your data changes. - 💡 Write Less Code
Define computed properties once—no more manual update logic!
Automatically recalculate properties when dependencies change.
Ideal for aggregations like counts, sums, or complex expressions.
modelBuilder.Entity<Person>().ComputedProperty(
p => p.NumberOfCats, // Property to map
p => p.Pets.Count(pet => pet.Type == PetType.Cat)); // Computed expression
Optimize performance by updating values without loading entire collections.
Perfect for large collections.
modelBuilder.Entity<Post>().ComputedProperty(
p => p.LikeCount, // Property to map
p => p.Interactions.Count(i => i.Type == InteractionType.Like), // Computed expression
c => c.NumberIncremental()); // Change calculation
Auto-sync related entities when their dependencies change.
Great for cloning relationships or maintaining derived collections.
modelBuilder.Entity<Order>().ComputedNavigation(
o => o.Items, // Navigation to map
o => o.CloneFrom != null
? o.CloneFrom.Items.Select(i => new OrderItem
{
Product = i.Product,
Quantity = i.Quantity
}).ToArray()
: o.AsComputedUntracked().Items, // Computed expression
c => c.CurrentValue(), // Change calculation
c => c.ReuseItemsByKey(e => new { e.Product })); // Other options
React to changes with event-driven callbacks.
Notify users, log data, or trigger workflows when values update.
modelBuilder.Entity<Person>().ComputedObserver(
p => p.Pets.Count(x => x.Type == PetType.Cat), // Observed expression
p => p.IsActive, // Filter
c => c.CurrentValue(), // Change calculation
async (person, numberOfCats) =>
{
Console.WriteLine($"Person {person.FullName} now has {numberOfCats} cats.");
}); // Callback
-
Install the Package:
dotnet add package LLL.AutoCompute.EFCore
-
Enable AutoCompute:
dbContextOptions.UseAutoCompute();
-
Map computed properties:
modelBuilder.Entity<Person>().ComputedProperty( p => p.NumberOfCats, // Property to map p => p.Pets.Count(pet => pet.Type == PetType.Cat)); // Computed expression
Done! Computed properties will now update automatically during dbContext.SaveChanges()
.
Auto Compute analyzes computed expressions and tracks all referenced data. When any of the referenced data changes, it traverses inverse navigations to identify affected entities and updates their computed properties accordingly.
- Computed properties, collections and references
- Incremental computed properties
- Incremental calculation
- Load all navigation items to evaluate Linq All/Any/Contains
- Load necessary navigation items to evaluate Linq Distinct
- Computed observers
- Async queue-based update for hot computed properties
- Throttled update - Update X seconds after the first change
- Debounced update - Update X seconds after the last change
- Methods to query inconsistent entities
- Periodic consistency check and fixes
- Web-based UI for schema introspection, consistency check and fix
MIT licensed. See LICENSE for details.