Hi,
The situation:
I have an existing customer table that is defined like this:
Table: Lookups
Id (int) [PK]
TypeId (nvarchar) [PK]
Order (int)
Description (nvarchar)
that other tables reference in a weak relation like this:
Table: Others
TypeIdlookupId (int)
Where **TypeId **is the value of the TypeId in the lookups table. A horrible way of defining relations, I know, but I can't change the database.
Still i would like to be able to have a property mapped on this relation and prefetchpath for the _LookupEntity _ on OtherEntity, so I've manually defined:
- A Property (LookupEntity TypeIdLookup)
- A Relation:
public virtual IEntityRelation TypeIdLookupUsingTypeIdlookupId
{
get
{
IEntityRelation relation = new EntityRelation(RelationType.ManyToOne, "TypeIdLookup", false);
relation.AddEntityFieldPair(LookupFields.Id, ContractFields.LegalHierarchyLookupId);
relation.CustomFilter = new PredicateExpression(LookupFields.TypeId=="TypeId");
relation.InheritanceInfoPkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("LookupEntity", false);
relation.InheritanceInfoFkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("OtherEntity", true);
return relation;
}
}
A Prefetch path:
public static IPrefetchPathElement2 PrefetchTypeIdLookup
{
get
{
return new PrefetchPathElement2(new EntityCollection(EntityFactoryCache2.GetEntityFactory(typeof(LookupEntityFactory))),
Relations.TypeIdLookupUsingTypeIdlookupId,
(int)EntityType.OtherEntity, (int)EntityType.LookupEntity, 0, null, new PredicateExpression(LookupFields.TypeId == "TypeId"), null, null, "TypeIdLookup", RelationType.ManyToOne);
}
}
Now the relation and the prefetch path work fine, the entity is fetched, however the related property is never set, because in DataAccessAdapterBase.MergeNormal line 6016
the FKHash (based on **TypeIdlookupId, 1 field) is obviously different from the PKhash (Id **and TypeId, 2 fields).
So, to make long story short, what I would like to do is define a new (runtime) field for OtherEntity that always returns a fixed value, and add this to my relation field mapping:
relation.AddEntityFieldPair(LookupFields.Id, ContractFields.LegalHierarchyLookupId);
relation.AddEntityFieldPair(LookupFields.TypeId, new FixedValueField("TypeId"));
Would you say this is the way to go, or is there an alternative solution?
Also, in the partial class of OtherEntity, I have to hook into this method:
public override void SetRelatedEntityProperty(string propertyName, IEntity2 entity){}
to add functionality like this:
switch(propertyName)
case "TypeIdLookup":
this.TypeIdLookup = (LookupEntity)entity;
break;
in order set my related entity. Which method of the _EntityBase2 _should I override, or event should I attach to in order to set this property?
Unfortunately I can't use and inherited business object, because I also use entity inheritance for OtherEntity.
Thank you in advance,
Wiebe Tijsma