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

Problem adding rows

1 Answer 40 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Leandro Vidal
Top achievements
Rank 1
Leandro Vidal asked on 20 Jan 2010, 12:03 PM
Hello. I have this code:

public void Save(Client client, List<Contact> contactList)
        {
            ObjectScopeProvider1.ObjectScope().Transaction.Begin();
            ObjectScopeProvider1.ObjectScope().Add(client);
            
            foreach (Contact c in contactList)
            {
                c.Client = client;
                ObjectScopeProvider1.ObjectScope().Add(c);
            }
            ObjectScopeProvider1.ObjectScope().Transaction.Commit();
        }

When I execute this code and I see the database, only the Client has been added, there's not any contact in the table. What's wrong?
Thanks!

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 22 Jan 2010, 06:28 PM
Hello Leandro Vidal,

Can you please share some more information regarding your model? Does your client object use Auto-increment mechanism for generating its key? If so, you should care for the following scenario:
The client object does not know its id until a round trip to the database is done (because the ID is generated by the database itself). Having that in mind, when setting the client reference to your Contact object the client has ID equaling 0. This might rise an exception and the entry will not be added. Please call ObjectScopeProvider1.ObjectScope().Transaction.Flush() after each insert. This will ensure that each entry does the necessary roundtrip to the database and thus knows its ID. Please note that the flush does not commit the transaction but rather temporary pushes the data to the server and allows you to perform a rollback at a later stage.

Sincerely yours,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Development (API, general questions)
Asked by
Leandro Vidal
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or