I have an order entry system in Silverlight. I have two tables – Orders and OrderItems. The OrderNo field in Orders table is autoincrement, but the OrderNo field in the OrderItems table is not.
What I need is whenever customer submits an order, the orderitem table’s orderno should also be filled with the orders table orderno.
What I do at the moment is add a new record to the orders table and get the incremented value and use it for the orderitem records. but, I dont want to do that way, I need these values to be generated simultaneously when the order table is updated.
Wrap your insert statements in a transaction. You will still use the same logic that you already have (updating the tables separetly), but since you are wrapping them in the same transaction, it will be an atomic operation from the database standpoint. So, if something goes wrong and you have to rollback, it will roll back both inserts, and no one will be able to observe the insert into Order until the whole transaction is committed.
EDIT
If you are already using Entity Framework, then it’s a transaction scope that you want. And while you are at it, I would advise that you take a really serious look at WCF RIA Services for Silverlight. It’s really a very good framework for database services.
2nd Edit
Here’s a video on RIA Services: http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/MID311