locked
Entity Framework AddTo Method fails with no error or exception RRS feed

  • Question

  • I have a single entity with no associations.  My ObjectContext class instantiates without error.

    When I use the AddTo method for a stand alone entity (an entity with zero associations) and I check the count of the rows in the entity in the context, I get zero rows.  All row values for the entity are specified.

    No error or exception is thrown.  Would anyone know any likely causes?

    Monday, August 20, 2012 2:44 PM

Answers

  • As Fernando said it can be hard to say exactly without more details and code, but I can take a quick guess.

    It is likely that the method you are using to get the count of rows is actually hitting the database directly, doing a count on the table, and only returning data that is in the database. This would not include the data you added until after you have called save changes.

    So the resolution would be to call SaveChanges before you count, or do a count that includes local objects in the context.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by MDKimzey Monday, August 20, 2012 5:47 PM
    Monday, August 20, 2012 4:49 PM
  • It looks like you are correct.  If I use the Count property of an entity in a context, it returns the currently in the database.

    However if I do an aggregate such as:

    Dim entityCount As Integer = Aggregate eObj As MyEntity In MyContext.MyEntitie Into Count()

    You get the total count in the local context.

    • Marked as answer by MDKimzey Monday, August 20, 2012 5:52 PM
    Monday, August 20, 2012 5:52 PM

All replies

  • Hi MDKimzey;

    It is difficult to say without see the code you are using. Please post the code.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, August 20, 2012 4:00 PM
  • As Fernando said it can be hard to say exactly without more details and code, but I can take a quick guess.

    It is likely that the method you are using to get the count of rows is actually hitting the database directly, doing a count on the table, and only returning data that is in the database. This would not include the data you added until after you have called save changes.

    So the resolution would be to call SaveChanges before you count, or do a count that includes local objects in the context.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by MDKimzey Monday, August 20, 2012 5:47 PM
    Monday, August 20, 2012 4:49 PM
  • It looks like you are correct.  If I use the Count property of an entity in a context, it returns the currently in the database.

    However if I do an aggregate such as:

    Dim entityCount As Integer = Aggregate eObj As MyEntity In MyContext.MyEntitie Into Count()

    You get the total count in the local context.

    • Marked as answer by MDKimzey Monday, August 20, 2012 5:52 PM
    Monday, August 20, 2012 5:52 PM