I am new to Linq to Sql. I got a scenario. I have two tables. MasterTable and DetailTable table. What I am trying to do is :
Inserting new rows in the DetailTable and on base of DetailTable rows I am trying to Update Master in One transaction.
Here is my code :
DBContext context = new DBContext();
context.Connection.Open();
context.Transaction = context.Connection.BeginTransaction();
DetailTable detail = new DetailTable();
detail.Amount = 100;
var detailTable = context.GetTable<DetailTable>();
// pass in the object with insert on submit
// and then submit changes
detailTable.InsertOnSubmit(detail);
var result = (from Total in context.MasterTable
select Total).Sum();
decimal total = (decimal)result; // This total is not the latest.
// UpdateMaster.....
// ................
context.SubmitChanges();
context.Transaction.Commit();
Now the problem, I am facing is that I am not getting the latest Sum from MasterTable. Like after inserting new row of amount 100, say I should get 600 but I am getting 500 (Sum rows as if I have not inserted new row).
Please let me know if this is possible using Linq to Sql if it is then how or I am trying to achieve something which is not possible.
Try to put the
context.SubmitChanges();above thedecimal total = (decimal)result;