Trouble getting data from a many-to-many relationship.

Posts   
 
    
Posts: 37
Joined: 09-Nov-2016
# Posted on: 10-Nov-2016 15:37:12   

Hi,

I am evaluating LLBLGen version 5.0.7 (and thus a total newbie) and everything have been going great so far, however I am have some troubles with a many-to-many relationship.

I have an inherited entity called "Fur". The "Fur" entity is the last in a hierarchy which at the top have an abstract entity called “Base”. The entity "Base" has a many-to-many relationship with an entity called "ActorHistory" via an entity called "CoBaseActorHistory". I am using the “Target per entity” inheritance type. See attached image for the model.

I am able in insert and query most data just fine, the problem is when I am trying to get all the "ActorHistory" entities related to a "Fur" entity.

My code looks like this:

FurEntity fur = null;

using (var adapter = new DataAccessAdapter(true))
{
    var metaData = new LinqMetaData(adapter);
    fur = metaData.Fur.SingleOrDefault(x => x.Collection.Name == "Some collection name");
    List<ActorHistoryEntity> allActorsRelatedToFur = fur.ActorHistoryCollectionViaCoBaseActorHistory.ToList();
}

I have tried calling:

adapter.FetchNewEntity<ActorHistoryEntity>(fur.GetRelationInfoActorHistoryCollectionViaCoBaseActorHistory());

Before getting the list, but that does not seem to help. I am not sure I understand either the design of the navigation properties of the many-to-many relationship (since I have both the original 1:m relationship needed to create the many-to-many relationship, and the many-to-many relationship) or the code needed to get the data.

Is there something wrong with my design, or am I missing some call to load data?

Best regards Andreas

Attachments
Filename File size Added on Approval
Model.png 31,077 10-Nov-2016 15:37.16 Approved
Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 10-Nov-2016 18:31:11   

It's much easier to use PrefetchPaths

var q = (from o in metaData.Order
         where o.EmployeeId == 1
         select o)
.WithPath(
     new PathEdge<ProductEntity>(
          OrderEntity.PrefetchPathProductCollectionViaOrderDetail));

Posts: 37
Joined: 09-Nov-2016
# Posted on: 14-Nov-2016 13:34:15   

Thank you for your reply. I found two ways.

To directly get the "ActorHistory" entities:


using (var adapter = new DataAccessAdapter(true))
{
    var metaData = new LinqMetaData(adapter);
    
    var fur = metaData.Fur.SingleOrDefault(x => x.Collection.Name == "Somename")

    var actorHistoryEntities = from coBaseHistory in metaData.CoBaseActorHistory where coBaseHistory.Base.Id == fur.Id select coBaseHistory.ActorHistory;
}

Or getting the "Fur" entity with the ActorHistory Entities populated:


var furs = (from o in metaData.Fur where o.Id == fur.Id select o).WithPath(p => p.Prefetch(c => c.ActorHistoryCollectionViaCoBaseActorHistory));

Best regards Andreas