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 )