How to use DynamicRelation for existing entities

Posts   
 
    
DannyGreen
User
Posts: 12
Joined: 25-Jul-2008
# Posted on: 27-Jan-2010 20:21:46   

Hello,

I am using LLBLGen 2.6 and SQL Server 2005.

I am wondering if it is possible to create a dynamic relation using 2 existing entities (I don't think I need a dynamic table), so that they join on fields that are not part of a relation that currently exists. The documentation seems to say you can, but I cannot find a clear example of how this would be done.

A DynamicRelation instance can be useful in the situation where you want to join two elements over a non-existing relation, e.g. you want to create a join between two entities by specifying a predicate expression

Here is what I've tried:


            RelationPredicateBucket bucket = new RelationPredicateBucket(EntityFieldFields.FieldSetId == -2100000000);          
            DynamicRelation relation = new DynamicRelation(EntityType.EntityFieldEntity, JoinHint.Left, 
                EntityType.FrameworkStringEntity, "ef", "fs", EntityFieldFields.Caption == FrameworkStringFields.StrnName);
            bucket.Relations.Add(relation);
            EntityCollection<EntityFieldEntity> collection = new EntityCollection<EntityFieldEntity>();
            using (var adp = _factory.CreateAdapter())
            {
                adp.FetchEntityCollection(collection, bucket);
            }


I know that I am missing something, because of course there is no place for the joined collection of FrameworkStringEntity to go. When I try to run I get many errors for every field indicating the multi-part identifier could not be bound.

Basically I'd simply like to mimic the behaviour I would have if I could create a Relation from within the LLBLGen designer, and there was an EntityCollection property on the EntityFieldEntity object with the related FrameworkStringEntity objects.

Thank you for any help.

Danny

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Jan-2010 06:13:10   

As I see, you don't need DynamicRelation, just a normal EntityRelation. DynamicRelations are useful is you have a complex relation like this:

SELECT o.*
FROM
(
    SELECT  CustomerId 
    FROM    Customers
    WHERE   Country = @country
) c INNER JOIN Orders o ON
  c.CustomerId = o.CustomerId

So, if you just want to link two fields in a relation, just do this:

EntityRelation relation = new EntityRelation(EntityFieldFields.Caption,
        FrameworkStringFields.StrnName, RelationType.OneToMany);

bucket.Relations.Add(relation);

Hope helpful wink

David Elizondo | LLBLGen Support Team
DannyGreen
User
Posts: 12
Joined: 25-Jul-2008
# Posted on: 28-Jan-2010 12:00:10   

This at least did not error, but I am not seeing how to access the related data. I have inspected all of these:


collection[0].GetRelatedData().Count
collection[0].GetDependingRelatedEntities().Count
collection[0].GetDependentRelatedEntities().Count
collection[0].GetAllRelations().Count
collection[0].GetMemberEntityCollections().Count

And all return 0.

Please note that neither the FieldEntityField.Caption, or FrameworkString.StrnName are FK or PK, which is why I could not create the entity using the designer. Does this matter?

I have this view which works fine, and I have an Entity that is based on this view, however in this case I want separate entities for the 2 tables to use for updating purposes.


SELECT ef.enfdId, ef.fdstId, ef.enfdFieldName, fs1.strnValue AS Caption, ef.enfdFormatting, ef.enfdHidden, ef.enfdWidth, ef.enfdMinWidth, ef.enfdMaxWidth, 
               ef.enfdZorder, ef.DateCreated, ef.LastModified, ef.ModifiedBy, fs1.applId, fs1.langId
FROM  dbo.foiEntityField AS ef LEFT OUTER JOIN
               dbo.foiFrameworkString AS fs1 ON ef.enfdCaption = fs1.strnName

Thanks! Danny

DannyGreen
User
Posts: 12
Joined: 25-Jul-2008
# Posted on: 28-Jan-2010 13:34:42   

Probably I need a PrefetchPath? I'm having trouble creating a new IPrefetchPathElement2 object though, not a lot of documentation on exactly what to supply for the arguments here. All examples use the PrefetchPath property of an entity, but I of course don't have that here.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Jan-2010 01:35:17   

You can't create prefetchPaths by code. PrefetchPaths are based on relations present in the LLBLGen Project (.lgp file). If the pk-side of relation you are creating is based on a PK, you can create such relation in the LLBLGen Designer. Otherwise, that is no possible.

What you can do is use the Entity based on the view for display data, and when the user clicks "Edit" you use the editable Entity for saving changes.

David Elizondo | LLBLGen Support Team