Otis wrote:
You mean, you want, via reflection, call someEntity.Relations.SomeRelation... ?
You can also create an EntityRelation in code, without reflection.
Just curious: why you need reflection to fetch a collection/creating a prefetchpath?
This is a method that was written for my by another developer and I am now trying to extend its functionality. What I am trying to do is move the sorting from entityProductVersionFetchPath to the adapter.FetchEntityCollection method and add sorting on two more fields. Now I believe that I need to create relations between the tables for the sort to work an exception will thrown stating that "The column prefix 'dbname.dbo.tablename' does nat match...". So I am now trying to create a relation based on the entityClass variable and was trying to use code similer to how the prefetch path entityProductFetchPath and can't make it work. Does this help?
public EntityCollection Execute() {
Assembly assembly = Assembly.GetAssembly(typeof(ProductEntity));
Type factoryType = assembly.GetType("WrenchScience.LLBLGen.FactoryClasses." + entityClass.Name + "Factory");
IEntityFactory2 factory = (IEntityFactory2)factoryType.GetConstructor(new Type[0]).Invoke(new object[0]);
EntityCollection products = new EntityCollection(factory);
EntityType entityType = (EntityType)Enum.Parse(typeof(EntityType), !entityClass.Name.StartsWith("My") ? entityClass.Name : entityClass.Name.Substring(2) /* skip "My" */);
IPrefetchPathElement2 entityProductFetchPath = (IPrefetchPathElement2)entityClass.GetProperty("PrefetchPathProduct").GetValue(null, new object[0]);
//IPrefetchPathElement2 entityManufacturerFetchPath = (IPrefetchPathElement2)entityClass.GetProperty("PrefetchPathManufacturer").GetGetMethod().Invoke(null, new object[0]);
IPrefetchPathElement2 entityProductVersionFetchPath = (IPrefetchPathElement2)entityClass.GetProperty("PrefetchPathProductVersion").GetValue(null, new object[0]);
ISortExpression sort = new SortExpression();
//sort.Add( SortClauseFactory.Create(ProductFieldIndex.ManufacturerID, SortOperator.Ascending) );
//sort.Add( SortClauseFactory.Create(ProductFieldIndex.Model, SortOperator.Ascending) );
sort.Add( SortClauseFactory.Create(ProductVersionFieldIndex.RPPrice, SortOperator.Ascending) );
sort.Add( SortClauseFactory.Create(ProductVersionFieldIndex.MfrGrams, SortOperator.Ascending) );
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)entityType);
IPrefetchPathElement2 productNode = prefetchPath.Add(entityProductFetchPath);
IPrefetchPathElement2 manufacturerNode = productNode.SubPath.Add(ProductEntity.PrefetchPathManufacturer);
manufacturerNode.SubPath.Add(MyManufacturerEntity.PrefetchPathCompany);
prefetchPath.Add(entityProductVersionFetchPath, 1, null, null, sort);
filter = new RelationPredicateBucket();
//filter.Relations.Add(ProductVersionEntity.Relations.ProductEntityUsingProductID);
//IRelationCollection relations = (IRelationCollection)entityClass.GetProperty("Relations").GetValue(null, new object[0]);
//IEntityRelation relation = (IEntityRelation)entityClass.GetProperty("Relations").GetValue(null, new object[0]);
//filter.Relations.Add(relation);
//filter.Relations.Add(BrakeEntity.Relations.ProductVersionEntityUsingProductVersionID);
foreach (ICriteria criteria in criterias) {
IPredicate expression = criteria.CreatePredicateExpression(logicManager, this);
if (expression != null)
filter.PredicateExpression.Add(expression);
}
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(products, filter, prefetchPath);
return products;
}