none
How to tell if an object had been persisted? RRS feed

  • Question

  • Dear All,

    In a method, there are many LINQ object been passing into the method.

    But some of them are from the database, and some are new (not persisted int the database yet).

    Is there a way for me to tell whether or not an object already in the DB?

    Thanks in advance.
    Tuesday, November 10, 2009 6:50 AM

Answers

  • Hi Chui,

    In LINQ to SQL, there is not a direct way to get the state of the certain entity.

    However, if you are using global DataContext, there is an workaround,
    First, you can get the changeset by using DataContext.GetChanges.
    Then get the inserted entities by ChangeSet.Inserts, etc.


    Best Regards
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun_Feng Monday, November 16, 2009 7:42 AM
    Thursday, November 12, 2009 6:52 AM
  • However, you won't be able to get objects that were retrieved from the database but have not been changed from DataContext.GetChangeSet, because the method will only return objects that will be inserted, updated, and deleted.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by Yichun_Feng Monday, November 16, 2009 7:42 AM
    Thursday, November 12, 2009 2:27 PM
    Answerer

All replies

  • Hi Chui,

    In LINQ to SQL, there is not a direct way to get the state of the certain entity.

    However, if you are using global DataContext, there is an workaround,
    First, you can get the changeset by using DataContext.GetChanges.
    Then get the inserted entities by ChangeSet.Inserts, etc.


    Best Regards
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun_Feng Monday, November 16, 2009 7:42 AM
    Thursday, November 12, 2009 6:52 AM
  • However, you won't be able to get objects that were retrieved from the database but have not been changed from DataContext.GetChangeSet, because the method will only return objects that will be inserted, updated, and deleted.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by Yichun_Feng Monday, November 16, 2009 7:42 AM
    Thursday, November 12, 2009 2:27 PM
    Answerer