Skip to content

3.2 Fluent Api

Bilal edited this page May 22, 2016 · 1 revision

If you don't like to put attributes on your entities, you can use the fluent api to configure tracking as following example.

        EntityTracker
            .TrackAllProperties<NormalModel>()
            .Except(x => x.Description)
            .And(x => x.Id);

Note that if you use both, annotations and fluent api, and they are conflicting for an entity or property, fluent api configuration will be considered high priority

Let's consider the following example where you have already configured tracking of a model with either annotations or fluent api. However now on runtime you want to override it for a specific property.

        EntityTracker
            .OverrideTracking<TrackedModelWithMultipleProperties>()
            .Disable(x => x.StartDate);

This way, all configuration is maintained and only StartDate tracking is disabled. You can enable it again using Enable() method.

You can configure tracking at 3 levels.

  1. Global
  2. Entity
  3. Property

Global Level example

Global level tracking is on by default but you can disable it at runtime as follows:

GlobalTrackingConfig.Enabled = false;

Entity Level example

Its very similar to Property level configuration -

for overriding entity level configuration,

        EntityTracker
            .TrackAllProperties<TrackedModelWithMultipleProperties>()
            .Except(x => x.Name)
            .And(x => x.Description);

        EntityTracker
            .OverrideTracking<TrackedModelWithMultipleProperties>()
            .Disable();

        EntityTracker
            .OverrideTracking<TrackedModelWithMultipleProperties>()
            .Enable();

In the above example, you specified tracking configuration for an entity and its properties. Then override the entity level tracking to be disabled while maintaining the tracking configuration and then enabled it again.

Note: While working with overrides, if you don't specify property, they work on entity on entity level

Clone this wiki locally