Relation Generation Template

Posts   
 
    
vassos
User
Posts: 32
Joined: 24-Jun-2008
# Posted on: 24-Jun-2008 10:35:21   

Hi, I'm using LLBLGen Pro v. 2.5. I have a template which generates me a method, which returns all relations of the entity. This template works fine, but it doesn't return the "Via" relations. These are "m:n" relations. Can you help me with some code for doing this, please? Here is what I have now (just a portion of the whole template):

/////////////////////////////////////////////////////////////// // This is generated code. ////////////////////////////////////////////////////////////// // Code is generated using LLBLGen Pro version: <[LLBLGenVersion]> // Code is generated on: <[Time]> // Code is generated using templates: <[TemplateName]> // Templates vendor: Solutions Design. // Templates version: <[TemplateVersion]> ////////////////////////////////////////////////////////////// using System; using System.Collections; using System.Collections.Generic; using <[RootNamespace]>; using <[RootNamespace]>.FactoryClasses; using <[RootNamespace]>.HelperClasses; using SD.LLBLGen.Pro.ORMSupportClasses;

namespace <[RootNamespace]>.RelationClasses { /// <summary>Implements the static Relations variant for the entity: <[CurrentEntityName]>. </summary> public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>Relations<[If Not IsInHierarchyType None]> : <[If IsSubType]><[SuperTypeName]>Relations<[Else]>IRelationFactory<[EndIf]><[EndIf]> { /// <summary>CTor</summary> public <[CurrentEntityName]>Relations() { }

    /// <summary>Gets all relations of the <[CurrentEntityName]>Entity as a list of IEntityRelation objects.</summary>
    /// <returns>a list of IEntityRelation objects</returns>
    public <[If IsSubType]>override<[Else]>virtual<[EndIf]> List<IEntityRelation> GetAllRelations()
    {
        List<IEntityRelation> toReturn = <[If IsSubType]>base.GetAllRelations();<[Else]>new List<IEntityRelation>();<[EndIf]>

<[Foreach RelatedEntity OneToMany CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]>);<[NextForeach]> <[Foreach RelatedEntity OneToOne CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[If IsOneToOnePkPk FkSide]><[RelationFieldName]><[EndIf]><[If Not IsOneToOnePkPk FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[If IsOneToOnePkFkUc FkSide]><[If RelatedEntityIsOtherEntity]><[RelationFieldName]><[EndIf]><[If Not RelatedEntityIsOtherEntity]><[RelatedEntityRelationFieldName]><[EndIf]><[EndIf]><[If Not IsOneToOnePkFkUc FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]>);<[NextForeach]> <[Foreach RelatedEntity ManyToOne CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[If Not RelatedEntityIsOtherEntity]><[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]><[EndIf]><[Foreach RelationField]><[RelationFieldName]><[NextForeach]>);<[NextForeach]> return toReturn; }

Thank you in advance!

vassos
User
Posts: 32
Joined: 24-Jun-2008
# Posted on: 24-Jun-2008 11:21:01   

It's me again simple_smile . I decided to paste you all the contents of the template cause I discovered that this template doesn't generate properties for the "Via" relations. See the comment with the exclamaition marks below.

I hope you can help.

/////////////////////////////////////////////////////////////// // This is generated code. ////////////////////////////////////////////////////////////// // Code is generated using LLBLGen Pro version: <[LLBLGenVersion]> // Code is generated on: <[Time]> // Code is generated using templates: <[TemplateName]> // Templates vendor: Solutions Design. // Templates version: <[TemplateVersion]> ////////////////////////////////////////////////////////////// using System; using System.Collections; using System.Collections.Generic; using <[RootNamespace]>; using <[RootNamespace]>.FactoryClasses; using <[RootNamespace]>.HelperClasses; using SD.LLBLGen.Pro.ORMSupportClasses;

namespace <[RootNamespace]>.RelationClasses { /// <summary>Implements the static Relations variant for the entity: <[CurrentEntityName]>. </summary> public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>Relations<[If Not IsInHierarchyType None]> : <[If IsSubType]><[SuperTypeName]>Relations<[Else]>IRelationFactory<[EndIf]><[EndIf]> { /// <summary>CTor</summary> public <[CurrentEntityName]>Relations() { }

    /// <summary>Gets all relations of the <[CurrentEntityName]>Entity as a list of IEntityRelation objects.</summary>
    /// <returns>a list of IEntityRelation objects</returns>
    public <[If IsSubType]>override<[Else]>virtual<[EndIf]> List<IEntityRelation> GetAllRelations()
    {
        List<IEntityRelation> toReturn = <[If IsSubType]>base.GetAllRelations();<[Else]>new List<IEntityRelation>();<[EndIf]>

<[Foreach RelatedEntity OneToMany CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]>);<[NextForeach]> <[Foreach RelatedEntity OneToOne CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[If IsOneToOnePkPk FkSide]><[RelationFieldName]><[EndIf]><[If Not IsOneToOnePkPk FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[If IsOneToOnePkFkUc FkSide]><[If RelatedEntityIsOtherEntity]><[RelationFieldName]><[EndIf]><[If Not RelatedEntityIsOtherEntity]><[RelatedEntityRelationFieldName]><[EndIf]><[EndIf]><[If Not IsOneToOnePkFkUc FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]>);<[NextForeach]> <[Foreach RelatedEntity ManyToOne CrLf]> toReturn.Add(this.<[RelatedEntityName]>EntityUsing<[If Not RelatedEntityIsOtherEntity]><[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]><[EndIf]><[Foreach RelationField]><[RelationFieldName]><[NextForeach]>);<[NextForeach]>

        return toReturn;
    }

    /// <summary>Gets all relations of the <[CurrentEntityName]>Entity as a list of KeyValuePairs with RelationEntityName string and IEntityRelation objects.</summary>
    /// <returns>a list of KeyValuePairs with RelationEntityName string and IEntityRelation objects.</returns>
    public <[If IsSubType]>override<[Else]>virtual<[EndIf]> List<KeyValuePair<string,IEntityRelation>> GetAllRelationsPlusEntityNames()
    {
        List<KeyValuePair<string,IEntityRelation>> toReturn = <[If IsSubType]>base.GetAllRelationsPlusEntityNames();<[Else]>new List<KeyValuePair<string,IEntityRelation>>();<[EndIf]>

<[Foreach RelatedEntity OneToMany CrLf]> toReturn.Add(new KeyValuePair<string,IEntityRelation>("<[RelatedEntityName]>Entity",this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]>));<[NextForeach]> <[Foreach RelatedEntity OneToOne CrLf]> toReturn.Add(new KeyValuePair<string,IEntityRelation>("<[RelatedEntityName]>Entity",this.<[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[If IsOneToOnePkPk FkSide]><[RelationFieldName]><[EndIf]><[If Not IsOneToOnePkPk FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[If IsOneToOnePkFkUc FkSide]><[If RelatedEntityIsOtherEntity]><[RelationFieldName]><[EndIf]><[If Not RelatedEntityIsOtherEntity]><[RelatedEntityRelationFieldName]><[EndIf]><[EndIf]><[If Not IsOneToOnePkFkUc FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]>));<[NextForeach]> <[Foreach RelatedEntity ManyToOne CrLf]> toReturn.Add(new KeyValuePair<string,IEntityRelation>("<[RelatedEntityName]>Entity",this.<[RelatedEntityName]>EntityUsing<[If Not RelatedEntityIsOtherEntity]><[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]><[EndIf]><[Foreach RelationField]><[RelationFieldName]><[NextForeach]>));<[NextForeach]> return toReturn; }

    #region Class Property Declarations

<[Foreach RelatedEntity OneToMany IncludeInherited CrLf]> /// <summary>Returns a new IEntityRelation object, between <[CurrentEntityName]>Entity and <[RelatedEntityName]>Entity over the 1:n relation they have, using the relation between the fields: <[Foreach RelationField CrLf]> /// <[CurrentEntityName]>.<[RelationFieldName]> - <[RelatedEntityName]>.<[RelatedEntityRelationFieldName]><[NextForeach]> /// </summary> public <[If RelationIsInherited]>override<[Else]>virtual<[EndIf]> IEntityRelation <[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]> { get { IEntityRelation relation = new EntityRelation(SD.LLBLGen.Pro.ORMSupportClasses.RelationType.OneToMany, "<[MappedFieldNameRelation]>" , true); <[Foreach RelationField CrLf]> relation.AddEntityFieldPair(<[CurrentEntityName]>Fields.<[RelationFieldName]>, <[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>);<[NextForeach]> relation.InheritanceInfoPkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("<[CurrentEntityName]>Entity", true); relation.InheritanceInfoFkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("<[RelatedEntityName]>Entity", false); return relation; } }<[NextForeach]> <[Foreach RelatedEntity OneToOne IncludeInherited CrLf]> /// <summary>Returns a new IEntityRelation object, between <[CurrentEntityName]>Entity and <[RelatedEntityName]>Entity over the 1:1 relation they have, using the relation between the fields: <[Foreach RelationField CrLf]> /// <[CurrentEntityName]>.<[RelationFieldName]> - <[RelatedEntityName]>.<[RelatedEntityRelationFieldName]><[NextForeach]> /// </summary> public <[If RelationIsInherited]>override<[Else]>virtual<[EndIf]> IEntityRelation <[RelatedEntityName]>EntityUsing<[Foreach RelationField]><[If IsOneToOnePkPk FkSide]><[RelationFieldName]><[EndIf]><[If Not IsOneToOnePkPk FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[If IsOneToOnePkFkUc FkSide]><[If RelatedEntityIsOtherEntity]><[RelationFieldName]><[EndIf]><[If Not RelatedEntityIsOtherEntity]><[RelatedEntityRelationFieldName]><[EndIf]><[EndIf]><[If Not IsOneToOnePkFkUc FkSide]><[RelatedEntityRelationFieldName]><[EndIf]><[NextForeach]> { get { IEntityRelation relation = new EntityRelation(SD.LLBLGen.Pro.ORMSupportClasses.RelationType.OneToOne, "<[MappedFieldNameRelation]>", <[If IsOnPkSide]>true<[Else]>false<[EndIf]>); <[Foreach RelationField]> <[If Not IsOneToOnePkPk FkSide]> relation.AddEntityFieldPair(<[CurrentEntityName]>Fields.<[RelationFieldName]>, <[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>);<[EndIf]> <[If Not IsOneToOnePkFkUc FkSide]> relation.AddEntityFieldPair(<[CurrentEntityName]>Fields.<[RelationFieldName]>, <[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>);<[EndIf]> <[If IsOneToOnePkPk FkSide]> relation.AddEntityFieldPair(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>, <[CurrentEntityName]>Fields.<[RelationFieldName]>);<[EndIf]> <[If IsOneToOnePkFkUc FkSide]> relation.AddEntityFieldPair(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>, <[CurrentEntityName]>Fields.<[RelationFieldName]>);<[EndIf]><[NextForeach]> relation.InheritanceInfoPkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo(<[If IsOnPkSide]>"<[CurrentEntityName]>Entity", true<[Else]>"<[RelatedEntityName]>Entity", false<[EndIf]>); relation.InheritanceInfoFkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo(<[If Not IsOnPkSide]>"<[CurrentEntityName]>Entity", true<[Else]>"<[RelatedEntityName]>Entity", false<[EndIf]>);<[EndIf]> return relation; } }<[NextForeach]>

!!!!!FROM HERE!!!!!

<[Foreach RelatedEntity ManyToOne IncludeInherited]> /// <summary>Returns a new IEntityRelation object, between <[CurrentEntityName]>Entity and <[RelatedEntityName]>Entity over the m:1 relation they have, using the relation between the fields: <[Foreach RelationField CrLf]> /// <[CurrentEntityName]>.<[RelationFieldName]> - <[RelatedEntityName]>.<[RelatedEntityRelationFieldName]><[NextForeach]> /// </summary> public <[If RelationIsInherited]>override<[Else]>virtual<[EndIf]> IEntityRelation <[RelatedEntityName]>EntityUsing<[If Not RelatedEntityIsOtherEntity]><[Foreach RelationField]><[RelatedEntityRelationFieldName]><[NextForeach]><[EndIf]><[Foreach RelationField]><[RelationFieldName]><[NextForeach]> { get { IEntityRelation relation = new EntityRelation(SD.LLBLGen.Pro.ORMSupportClasses.RelationType.ManyToOne, "<[ MappedFieldNameRelation ]>", false); <[Foreach RelationField CrLf]> relation.AddEntityFieldPair(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>, <[CurrentEntityName]>Fields.<[RelationFieldName]>);<[NextForeach]> relation.InheritanceInfoPkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("<[RelatedEntityName]>Entity", false); relation.InheritanceInfoFkSideEntity = InheritanceInfoProviderSingleton.GetInstance().GetInheritanceInfo("<[CurrentEntityName]>Entity", true); return relation; } }<[NextForeach]>

!!!!!TO HERE!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!Should have some property section (Like the one between !!!!!FROM HERE!!!!! and !!!!!TO HERE!!!!!) starting with <[Foreach RelatedEntity ManyToMany IncludeInherited]>!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

<[If IsInHierarchyType TargetPerEntity]><[Foreach RelatedHierarchyEntity]> /// <summary>Returns a new IEntityRelation object, between <[CurrentEntityName]>Entity and <[RelatedEntityName]>Entity over the 1:1 relation they have, which is used to build a target per entity hierarchy, and is using the relation between the fields: <[Foreach RelationField CrLf]> /// <[CurrentEntityName]>.<[RelationFieldName]> - <[RelatedEntityName]>.<[RelatedEntityRelationFieldName]><[NextForeach]> /// </summary> internal IEntityRelation RelationTo<[If IsOnSubTypeSide]>SuperType<[Else]>SubType<[EndIf]><[RelatedEntityName]>Entity { get { IEntityRelation relation = new EntityRelation(SD.LLBLGen.Pro.ORMSupportClasses.RelationType.OneToOne, <[If IsOnPkSide]>true<[Else]>false<[EndIf]>); <[Foreach RelationField]> <[If Not IsOneToOnePkPk FkSide]> relation.AddEntityFieldPair(<[CurrentEntityName]>Fields.<[RelationFieldName]>, <[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>);<[EndIf]> <[If Not IsOneToOnePkFkUc FkSide]> relation.AddEntityFieldPair(<[CurrentEntityName]>Fields.<[RelationFieldName]>, <[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>);<[EndIf]> <[If IsOneToOnePkPk FkSide]> relation.AddEntityFieldPair(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>, <[CurrentEntityName]>Fields.<[RelationFieldName]>);<[EndIf]> <[If IsOneToOnePkFkUc FkSide]> relation.AddEntityFieldPair(<[RelatedEntityName]>Fields.<[RelatedEntityRelationFieldName]>, <[CurrentEntityName]>Fields.<[RelationFieldName]>);<[EndIf]><[NextForeach]> return relation; } }<[NextForeach]> /// <summary>Returns the relation object the entity, to which this relation factory belongs, has with the subtype with the specified name</summary> /// <param name="subTypeEntityName">name of direct subtype which is a subtype of the current entity through the relation to return.</param> /// <returns>relation which makes the current entity a supertype of the subtype entity with the name specified, or null if not applicable/found</returns> public <[If IsSubType]>override<[Else]>virtual<[EndIf]> IEntityRelation GetSubTypeRelation(string subTypeEntityName) { <[If Not IsSuperType ]> return null;<[Else]> switch(subTypeEntityName) {<[Foreach RelatedHierarchyEntity]><[If Not IsOnSubTypeSide]> case "<[RelatedEntityName]>Entity": return this.RelationToSubType<[RelatedEntityName]>Entity;<[EndIf]><[NextForeach]> default: return null; }<[ EndIf ]> }

    /// <summary>Returns the relation object the entity, to which this relation factory belongs, has with its supertype, if applicable.</summary>
    /// <returns>relation which makes the current entity a subtype of its supertype entity or null if not applicable/found</returns>
    public <[If IsSubType]>override<[Else]>virtual<[EndIf]> IEntityRelation GetSuperTypeRelation()
    {

<[If IsSubType]> return this.RelationToSuperType<[SuperTypeName]>Entity;<[Else]> return null;<[ EndIf ]> } <[Else]> /// <summary>stub, not used in this entity, only for TargetPerEntity entities.</summary> public <[If IsSubType]>override<[Else]>virtual<[EndIf]> IEntityRelation GetSubTypeRelation(string subTypeEntityName) { return null; } /// <summary>stub, not used in this entity, only for TargetPerEntity entities.</summary> public <[If IsSubType]>override<[Else]>virtual<[EndIf]> IEntityRelation GetSuperTypeRelation() { return null;} <[EndIf]> #endregion

    #region Included Code

<# Custom_EntityRelationsTemplate #> #endregion } }

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 24-Jun-2008 11:59:10   

Would you please attach it as a file?

vassos
User
Posts: 32
Joined: 24-Jun-2008
# Posted on: 24-Jun-2008 12:36:54   

Walaa wrote:

Would you please attach it as a file?

Yes, sorry for the flood simple_smile

Attachments
Filename File size Added on Approval
entityRelations.template 13,329 24-Jun-2008 12:37.50 Approved
Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 25-Jun-2008 17:40:53   

If you want to have m:n relations as Entityrelation objects. That's not possible, as m:n relations are always a combination of 2 EntityRelation objects. So 'm:n' relations aren't creatable as a single EntityRelation object.

vassos
User
Posts: 32
Joined: 24-Jun-2008
# Posted on: 26-Jun-2008 09:17:05   

I found a workaround and solved my problem... for now!

Thanks anyway.

Posts: 27
Joined: 16-Jul-2007
# Posted on: 25-Nov-2008 20:54:51   

vassos wrote:

I found a workaround and solved my problem... for now!

Thanks anyway.

which was....... ?!? ;-)