I’m having trouble inserting related rows into my database. When I insert new objects into the database in a ASP.NET application, I get a foreign key violation error, whereas in a console application, I do not.
Here are the steps to reproduce the problem:
-
Create and run the following T-SQL script on a new database:
create table dbo.[Groups] ( GroupId int identity primary key, Name nvarchar(64) not null ) create table dbo.[Objects] ( ObjectId int identity primary key, GroupId int not null foreign key references [Groups] on delete cascade, Name nvarchar(64) not null ) -
Setup a C# console application named “ConsoleProgram”.
- Add a new DataSet named “dsObjects” to the project.
- Drag tables “Groups” and “Objects” from the server explorer into the designer.
- Edit the relation between the two tables and set the relation to “both relation and foreign key constraint” and update rule to “Cascade”.
-
Insert the following code into “Main”:
// (assuming appropriate using statement for dsObjectsTableAdapters) var ds = new dsObjects(); var mgr = new dsObjectsTableAdapters.TableAdapterManager { GroupsTableAdapter = new GroupsTableAdapter(), ObjectsTableAdapter = new ObjectsTableAdapter() }; mgr.GroupsTableAdapter.Fill( ds.Groups ); mgr.ObjectsTableAdapter.Fill( ds.Objects ); var row = ds.Groups.AddGroupsRow( "Fruits" ); ds.Objects.AddObjectsRow( row, "Apple" ); ds.Objects.AddObjectsRow( row, "Banana" ); ds.Objects.AddObjectsRow( row, "Orange" ); mgr.UpdateAll( ds ); -
Run program and verify the new rows were inserted into the respective tables:
GroupId Name ----------- ------- 1 Fruits (1 row(s) affected) ObjectId GroupId Name ----------- ----------- ------- 1 1 Apple 2 1 Banana 3 1 Orange (3 row(s) affected) -
Delete the newly created data.
- Close the solution.
- Create a new ASP.NET Web Application.
- Repeat steps 3-5.
- Insert the code in step 6 into the Page_Load function of Default.aspx.
- Run the program.
- Note the exception about the foreign key violation (if any).
- Run the console program again and note the data has been created successfully.
- Recreate the database and run the ASP.NET application again and note the same exception.
I have tried updating the tables sequentially over using the UpdateAll method, with the same results in both projects.
I am at a complete loss as to why this would work in a ConsoleApplication, but not an ASP.NET application. Any ideas?
Okay, after some searching around, I found the solution to my problem. In the thread, http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4d10fe87-2de8-42a8-8ef9-b9d46c0fd28d, it was hinted that in order for identity columns to be updated correctly in the dataset, a SELECT statement must be used after the INSERT statement. After exploring the “TableAdapter configuration Wizard”, I found that under the “Advanced Options” there is a “refresh the data table” option which adds the appropriate SELECT statement after INSERTions. For some unknown reason to me, this was unchecked by default in ASP.NET web applications and checked by default in the console application.
After checking this option, the dataset update works as expected.