Completed
Last Updated: 29 Apr 2022 14:19 by ADMIN
Release LIB 2022.1.502 (2 May 2022)
Martin Ivanov
Created on: 30 Mar 2022 12:47
Category: EntityFrameworkCoreDataSource
Type: Bug Report
1
EntityFrameworkCoreDataSource: NullReferenceException is thrown when using the QueryableEntityCoreCollectionView class

NullReferenceException is thrown with the following stacktrace when you initialize QueryableEntityCoreCollectionView.

at Telerik.Windows.Data.QueryableProxy`1.get_Count()
   at Telerik.Windows.Data.QueryableEntityCoreCollectionView`1.UpdateTotalItemCount()
   at Telerik.Windows.Data.QueryableCollectionView..ctor(IEnumerable sourceCollection, Type itemType)
   at Telerik.Windows.Data.QueryableCollectionView..ctor(IEnumerable source)
   at Telerik.Windows.Data.QueryableEntityCoreCollectionView`1..ctor(DbContext dbContext, IQueryable`1 query, Collection`1 include)

To work this around, you can create a custom QueryableEntityCoreCollectionView class and override its UpdateTotalItemCount() method.

public class CustomQueryableEntityCoreCollectionView<T> : QueryableEntityCoreCollectionView<T> where T : class, new()
{

	public CustomQueryableEntityCoreCollectionView(DbContext dbContext, IQueryable<T> query, Collection<string> include)
		: base(dbContext, query, include)
	{
	}

	protected override void UpdateTotalItemCount()
	{
		var sourceListProperty = typeof(QueryableCollectionView).GetProperty("SourceList", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
		var sourceList = (IList)sourceListProperty.GetValue(this);
		if (sourceList != null && !(sourceList is ICollectionView))
		{
			this.TotalItemCount = sourceList.Count;
		}
		else
		{
			var proxyProperty = typeof(QueryableEntityCoreCollectionView<T>).GetProperty("Proxy", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
			var proxy = proxyProperty.GetValue(this);
			if (proxy != null)
			{
				var dbSetProperty = proxy.GetType().GetProperty("DbSet");
				var dbSet = dbSetProperty.GetValue(proxy) as DbSet<T>;
				if (dbSet != null)
				{
					this.TotalItemCount = dbSet.Local.Count;
				}
				else
				{
					this.TotalItemCount = this.QueryableSourceCollection.Count();
				}
			}
		}
	}
}

0 comments