Ask a questionAsk a question
 

AnswerEF, savechanges() and InvalidOperationException

  • Friday, November 06, 2009 10:51 AMLopezan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,

    I use VS2008 SP1 and .NET 3.5.

    I have created an EF model from an existing DB with only one table like the following:

    ID INTEGER
    CODE VARCHAR
    DESCRIPTION VARCHAR

    This table has an ID (primary key) with a trigger that autogenerate his values.
    I have bind this table to a gridview.
    After every insert,change or delete row, I call the savechanges method to save data.

    But I have this problem.
    The first time I insert a row, all works fine.
    The next times, the savechanges method raise the exception:

    InvalidOperationException

    and if I go on, the last row is saved but also the previous rows are saved again.

    So, using the grid, if I insert the rows with this value:

    CODE DESCRIPTION
    1 1
    2 2
    3 3
    4 4
    5 5

    in the table I have:

      ID CODE DESCRIPTION
      1 1                1                                                                                                    
      2 2                2                                                                                                    
      3 2                2                                                                                                    
      5 2                2                                                                                                    
      9 2                2                                                                                                    
      4 3                3                                                                                                    
      6 3                3                                                                                                    
      8 3                3                                                                                                    
      7 4                4                                                                                                    
     11 4               4                                                                                                    
     10 5               5                                                                                                    


    Finally, the ID is generated correctly but the rows are duplicated.
    Please, help me.

    Regards.
    Angelo.

Answers

  • Friday, November 06, 2009 6:02 PMNoam Ben-Ami - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Can you provide the stack trace for the exception?

    I'm guessing that this may have something to do with the generation of the Id field: The EF may not be picking up the fact that the database autogenerates Ids, since they are done using a trigger, and so may not be doing the right thing in terms of reading back the generated Id values. The first thing I'd try (not seeing your trace) is to use an Identity declaration for the Id field, rather than a trigger - assuming your target database supports this.
    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Friday, November 06, 2009 1:05 PMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you show the code you use for the save changes?
    In addition, please explain what kind of application you use (win form, asp, WPF...) and how you bind your grid to the data
    Please mark posts as answers/helpful if it answers your question
  • Friday, November 06, 2009 1:44 PMLopezan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I use a win form application.
    The code to bind is:

                gridControl1.DataSource = m_context.TB_UM.Execute(System.Data.Objects.MergeOption.AppendOnly);

    and the code to save changes is:

                m_context.SaveChanges();


  • Friday, November 06, 2009 6:02 PMNoam Ben-Ami - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Can you provide the stack trace for the exception?

    I'm guessing that this may have something to do with the generation of the Id field: The EF may not be picking up the fact that the database autogenerates Ids, since they are done using a trigger, and so may not be doing the right thing in terms of reading back the generated Id values. The first thing I'd try (not seeing your trace) is to use an Identity declaration for the Id field, rather than a trigger - assuming your target database supports this.
    This posting is provided "AS IS" with no warranties, and confers no rights.