Question

I have an entity that has 1:n or n:m relationships to other entities but instead of doing a .Count() call/query against the database whenever needed, I was wondering whether there's any EF (6) built-in way to denormalize and write these frequently (in my case) accessed count numbers into a property/column once when such relationships are added/removed?

Is there any way to hook into a DbContext's DbSet.Add() / .Remove() mechanisms to do that or any other, maybe more elegant way?

Was it helpful?

Solution

There is no built-in way in EF.

My suggestion is to create a computed column that calculates the relationship count in the DB. That would take care of data retrieval of the count without having to eager/lazy load the collection but not update the count when you perform client-side operations to the collection prior to saving changes. To track changes client-side, I suggest implementing the collection nav property as an ObservableCollection<T>. Handled the CollectionChanged event to update the POCO's RelationshipCount property accordingly.

POCO properties and methods:

public virtual ObservableCollection<RelatedEntity> RelatedEntities { get; set; }

[DatabaseGenerated( DatabaseGeneratedOption.Computed )]
public int RelationshipCount { get; set; }

void RelatedEntities_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
    var col = sender as ReadOnlyObservableCollection<RelatedEntity>;

    RelatedEntities = col.Count();
}

Create a UDF in your DB to calculated the count value:

create function dbo.udfGetRelationshipCount( @id int )
returns int
as
    declare @count int

    select @count = count(*)
    from
        dbo.RelatedEntity
    where
        EntityId = @id

    return @count

Add column:

alter table dbo.Entity
    ADD RelationshipCount as dbo.udfGetRelationshipCount( EntityId )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top