This is a migrated thread and some comments may be shown as answers.

Saving one to many entities

2 Answers 93 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Erik
Top achievements
Rank 2
Erik asked on 03 Apr 2011, 01:44 PM
Hi,

Just want to see what the best approach would be for the flowing scenario:

I Have a table ListSelections and a table ListSelectionColumns in SQL Server 2008. Of course the last table has a Foreign key to the first table. In this scenario there is always 1 ListSelection record that can have 1 or more ListSelectionColumn records. 
Both tables have a primary key with an autonumber.

When implementing this in a model, i want to create a new ListSelectionEntity with 3 new ListSelectionColumnEntities:
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Using DC As New EntitiesModel
        Dim lse As New ListSelectionEntity
        lse.Name = TextBox1.Text
        lse.Description = "@: " & Format(Now, "yyyy-MM-dd HH:mm:ss")
        For i = 1 To 3
            Dim lsce As New ListSelectionColumnEntity
            lsce.Name = TextBox1.Text & " - Col " & i.ToString
            lsce.Remarks = "@: " & Format(Now, "yyyy-MM-dd HH:mm:ss")
            lse.ListSelectionColumnEntities.Add(lsce)
        Next
        DC.Add(lse)
        DC.SaveChanges()
    End Using
End Sub
This code generates all the records, 1 ListSelection and 3 ListSelectionColumns records, but the link between them is wrong. The value of the child records, the foreign key, is NULL in stead of the new assigned autonumber from the listSelection.

I discovered this because I used code from august 2010, that errored out; I know it did work in august. Now, instead, I save the ListSelectionEntity first, and than assign the new id to the child objects.

I provided a sample project (web app) using Q1 2011, latest build:
https://www.proovit.com/Download/Telerik/Telerik.TestEntityOne2anySaveWebApp1.zip

So, what would be the right approach for this? I was expecting that OA did update the foreign key.

Ok, thanks in advance,

Erik

2 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 04 Apr 2011, 09:22 AM
Hi Erik,

I think it is because the 1:m relationship is not managed (that is the default setting now if you do not set it explicitly - before it was default managed).

Open the relation in the designer and tick the managed setting.

Using a managed relationship OA will keep the relationship ends in sync, but this housekeeping has some drawbacks, especially when you have a large number of objects with this relationship.

You should really make it a good practice to add objects to the scope/context as soon as possible, because when it is added to the scope/context OA have more control over it and can do some more "magic" to the objects...

So change your code to the following first (without changing the relationship to be Managed):

rivate Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Using DC As New EntitiesModel
        Dim lse As New ListSelectionEntity
DC.Add(lse); // ListSelectionEntity is added as soon as possible to let OA control it
        lse.Name = TextBox1.Text
        lse.Description = "@: " & Format(Now, "yyyy-MM-dd HH:mm:ss")
        For i = 1 To 3
            Dim lsce As New ListSelectionColumnEntity
            lsce.Name = TextBox1.Text & " - Col " & i.ToString
            lsce.Remarks = "@: " & Format(Now, "yyyy-MM-dd HH:mm:ss")
            lse.ListSelectionColumnEntities.Add(lsce)
        Next
       
        DC.SaveChanges()
    End Using
End Sub


Regards

Henrik
0
Damyan Bogoev
Telerik team
answered on 07 Apr 2011, 05:14 PM
Hello Erik,

You could follow the approach suggested by Henrik.
If any other questions arise, do not hesitate to contact us back.
@Henrik: Thank you for your help.

Kind regards,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Erik
Top achievements
Rank 2
Answers by
IT-Als
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or