Add entity to m:n relation (in memory!)

Posts   
 
    
hypo
User
Posts: 34
Joined: 14-Oct-2008
# Posted on: 29-Oct-2008 17:47:25   

Hi all,

I have an entity Assessment and an entity Comment. The entities are connected to eachother through an m:n relation. (The middle-table (=the one holding the 2 ID's) is called AssessmentComments)

In WPF, I have a gridview, which binds to the Assessment.CommentsViaAssessmentComments (this is the readonly collection created by llblgen).

Now I want to add a comment In Memory. I mean, I don't want to save to the database yet!!!

I read the documentation. More in particular the paragraph: How do I add an entity A to an entity B's collection of A's if A and B have an m:n relation ?. So I use this same method without saving the entity. But this way, there is one entity added to the AssessmentComments entityCollection, but the CommentsViaAssessmentComments entityCollection isn't updated! This way the wpf UI doesn't show the update.

Some code: (codebehind)

void View_OnAddComment(object sender,EventArgs<string> e)
{
    //create a CommentsEntity
    CommentsEntity vCommentEntity = new CommentsEntity();
    vCommentEntity.UserId = mAuthenticationService.UserId;
    vCommentEntity.Timestamp = DateTime.Now;
    vCommentEntity.Comment = e.Data;

    //link the AssessmentsEntity and the CommentsEntity 
    AssessmentCommentsEntity vAssessmentCommentsEntity = new AssessmentCommentsEntity();
    vAssessmentCommentsEntity.Assessments = View.Model.AssessmentFile.Assessments;
    vAssessmentCommentsEntity.Comments = vCommentEntity;

    View.Model.AssessmentFile.Assessments.AssessmentComments.Add(vAssessmentCommentsEntity);
}

(xaml)

<ListView Grid.Row="0" Grid.ColumnSpan="4" 
          ItemsSource="{Binding AssessmentFile.Assessments.CommentsCollectionViaAssessmentComments}"
          IsSynchronizedWithCurrentItem="True"  >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Timestamp}" />
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=UserId}" />
            <GridViewColumn Header="Comment" DisplayMemberBinding="{Binding Path=Comment}" />
        </GridView>
    </ListView.View>
</ListView>

<Label Grid.Row="1" Grid.Column="0" FlowDirection="RightToLeft">:Add Comment</Label>
<TextBox Name="txtVehicleAddComment" Grid.Row="1" Grid.Column="1" />
<Button Name="btnVehicleAddComment" Click="AddComment" Grid.Row="1" Grid.Column="2" Width="50">+</Button>

Summerized: Q: How do I add an entity to an entitycollection that was created by an m:n-relation without saving to the database?

If you have any questions please let me know!

Kind regards, Wim

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 30-Oct-2008 03:07:55   

Try this. (remember, this will work in-memory only):

CommentsEntity vCommentEntity = new CommentsEntity();
vCommentEntity.UserId = mAuthenticationService.UserId;
vCommentEntity.Timestamp = DateTime.Now;
vCommentEntity.Comment = e.Data;

View.Model.AssessmentFile.Assessments.CommentsViaAssessmentComments.IsReadOnly = false;
View.Model.AssessmentFile.Assessments.CommentsViaAssessmentComments.Add(vCommentEntity);
David Elizondo | LLBLGen Support Team
hypo
User
Posts: 34
Joined: 14-Oct-2008
# Posted on: 30-Oct-2008 11:33:43   

Hi daelmo,

The code you gave me is working.

The saving also works, but then I have to do the following when saving in memory:

Both the InBetweenTable's entities and the m:n relation entities have to be prefetched: part of the prefetch:

assessmentFile => assessmentFile
    .Prefetch<AssessmentsEntity>(c => c.Assessments)
    .SubPath
    (
        assessment => assessment
            .Prefetch<AssessmentCommentsEntity>(c => c.AssessmentComments)
            .Prefetch<CommentsEntity>(c => c.CommentsCollectionViaAssessmentComments)

So the saving In memory is done like this:

//create a CommentsEntity
CommentsEntity vCommentEntity = new CommentsEntity();
vCommentEntity.UserId = mAuthenticationService.UserId;
vCommentEntity.Timestamp = DateTime.Now;
vCommentEntity.Comment = e.Data;
View.Model.AssessmentFile.Assessments.CommentsCollectionViaAssessmentComments.IsReadOnly = false;
View.Model.AssessmentFile.Assessments.CommentsCollectionViaAssessmentComments.Add(vCommentEntity);

//link the AssessmentsEntity and the CommentsEntity 
AssessmentCommentsEntity vAssessmentCommentsEntity = new AssessmentCommentsEntity();
vAssessmentCommentsEntity.Assessments = View.Model.AssessmentFile.Assessments;
vAssessmentCommentsEntity.Comments = vCommentEntity;
View.Model.AssessmentFile.Assessments.AssessmentComments.Add(vAssessmentCommentsEntity);

The saving to the database works like this (the comments are sub-entities of the AssessmentsEntity):

adapter.SaveEntity(aAssessmentsEntity, true, true);

Thanks again for the great support wink Kind regards, Wim