When are the entities available in the context after add?

2 posts, 1 answers
  1. Alex
    Alex avatar
    27 posts
    Member since:
    Oct 2011

    Posted 10 Apr 2012 Link to this post

    Basically, I'd like to understand why this scenario fails:

    context.Add(cisPart);
    List<AudioProductsBuzzers> list = context.AudioProductsBuzzers.ToList<AudioProductsBuzzers>();

    Where cisPart is an AudioProductBuzzers entity. The newly added instance does not appear in the list.

    Regards,

    Alex
  2. Answer
    Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 12 Apr 2012 Link to this post

    Hi Alexis,

    The reason why the new object is not presented in the list is that it is not yet saved to the database. When you add an object to the context it is not automatically accessible through the collection, it is only marked for insertion in the database.

    You have two options - either call SaveChanges() or FlushChanges() before the moment you need to access the new object through the context, so all the executed queries will consider it. Before that, you should just keep using the newly created object.

    You can find some more details and examples in the Context API guide for handling transactions and performing CRUD operations.

    I hope that clarifies the matter. Do not hesitate to get back to us if you have any questions.
     

    Regards,
    Ivailo
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  3. DevCraft banner
Back to Top