Data Platform Developer Center >
Data Platform Development Forums
>
ADO.NET Entity Framework and LINQ to Entities
>
EF, savechanges() and InvalidOperationException
EF, savechanges() and InvalidOperationException
- 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 INTEGERCODE VARCHARDESCRIPTION VARCHARThis 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:InvalidOperationExceptionand 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 DESCRIPTION1 12 23 34 45 5in the table I have:ID CODE DESCRIPTION1 1 12 2 23 2 25 2 29 2 24 3 36 3 38 3 37 4 411 4 410 5 5Finally, the ID is generated correctly but the rows are duplicated.Please, help me.Regards.Angelo.
- Moved byVMazurMVP, ModeratorFriday, November 06, 2009 11:11 AM (From:ADO.NET Data Providers)
Answers
- 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.- Marked As Answer byYichun_FengMSFT, ModeratorThursday, November 12, 2009 2:29 AM
All Replies
- 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 - 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();
- 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.- Marked As Answer byYichun_FengMSFT, ModeratorThursday, November 12, 2009 2:29 AM


