Problem adding rows

2 posts, 0 answers
  1. Leandro Vidal
    Leandro Vidal avatar
    3 posts
    Member since:
    Jan 2010

    Posted 20 Jan 2010 Link to this post

    Hello. I have this code:

    public void Save(Client client, List<Contact> contactList)
                foreach (Contact c in contactList)
                    c.Client = client;

    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?
  2. PetarP
    PetarP avatar
    754 posts

    Posted 22 Jan 2010 Link to this post

    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,
    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.
  3. DevCraft banner
Back to Top