Failed SubmitChanges()
Posted: Wed 30 Mar 2011 21:30
Hello!
Here is a scenario, where SubmitChanges() fails with DB exception.
Use three tables (in my case i used SQL Server 2008):
R1(Users) = {ID(PK, int, identity), Name(varchar(100), not null)}
R2(Orders) = {ID(PK, int, identity), UserID(FK, int), StatusID(FK, int), SomeData}
R3(Status) = {ID(PK, int, identity), Name(varchar(100), not null}
The following code will lead to exception on SubmitChanges() with message "The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Orders_Users\". The conflict occurred in database \"TEST\", table \"dbo.Users\", column 'ID'.\r\nThe statement has been terminated."
But the next code will go fine:
The next code works, but object remains in BindingList:
Thanks
Here is a scenario, where SubmitChanges() fails with DB exception.
Use three tables (in my case i used SQL Server 2008):
R1(Users) = {ID(PK, int, identity), Name(varchar(100), not null)}
R2(Orders) = {ID(PK, int, identity), UserID(FK, int), StatusID(FK, int), SomeData}
R3(Status) = {ID(PK, int, identity), Name(varchar(100), not null}
The following code will lead to exception on SubmitChanges() with message "The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Orders_Users\". The conflict occurred in database \"TEST\", table \"dbo.Users\", column 'ID'.\r\nThe statement has been terminated."
Code: Select all
using (TestDataContext ctx = new TestDataContext())
{
IBindingList bl = ctx.Users.First().Orders.GetNewBindingList();
Order order = new Order();
order.Status = ctx.Statuses.First();
bl.Add(order);
bl.Remove(order);
ctx.SubmitChanges();
}
Code: Select all
using (TestDataContext ctx = new TestDataContext())
{
IBindingList bl = ctx.Users.First().Orders.GetNewBindingList();
Order order = new Order();
// order.Status = ctx.Statuses.First();
bl.Add(order);
bl.Remove(order);
ctx.SubmitChanges();
Code: Select all
using (TestDataContext ctx = new TestDataContext())
{
IBindingList bl = ctx.Users.First().Orders.GetNewBindingList();
Order order = new Order();
order.Status = ctx.Statuses.First();
bl.Add(order);
order.Status = null;
ctx.SubmitChanges();