Skip to main content
All docs
V24.2
.NET 8.0+

Change Tracking in EF Core DbContext and Performance Considerations

  • 2 minutes to read

In XAF, it is important that the application’s UI is notified when a business object’s property has been changed so that the displayed data is updated accordingly. For this reason, projects created by the XAF Solution Wizard use the ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues change tracking strategy. This strategy requires entity classes to implement the INotifyPropertyChanged and INotifyPropertyChanging interfaces.

XAF’s built-in classes do not implement the INotifyPropertyChanged and INotifyPropertyChanging interfaces. Instead, the change-tracking proxies extension is used so that the ChangeTrackingStrategy.ChangingAndChangedNotificationsWithOriginalValues strategy works correctly. This extension requires that all persistent properties in your persistent classes are declared as virtual:

public class Department : BaseObject {
    public virtual string Name { get; set; }
    // ...
}

How to Use Non-Virtual Properties

If you need to use classes with non-virtual persistent properties, the change tracking strategy requires you to manually implement the INotifyPropertyChanged and INotifyPropertyChanging interfaces for all classes that you use in your DBContext, including XAF internal classes.

As an alternative, you can use the ChangeTrackingStrategy.Snapshot strategy so your entity classes do not need to implement INotifyPropertyChanged and INotifyPropertyChanging. However, this can lead to performance issues and cause the application’s UI to incorrectly refresh the displayed data.

Use a DbContext Outside of an XAF Application

If you develop a non-XAF application that needs to reuse your XAF application’s DbContext, make sure to add the change-tracking proxies extension in the DbContext creation code:

// ...
var optionsBuilder = new DbContextOptionsBuilder<MyEFCoreDbContext>()
    .UseSqlServer(connectionString)
    .UseChangeTrackingProxies();
var db = new MyEFCoreDbContext(optionsBuilder.Options);
// ...
See Also