Penguen
Penguen

Reputation: 17268

Updating my table via EF 4.0Entity Exception occured

i writed these codes to update my whole table re order my jobseqno:


      protected void Update()
        {
            MyEntities ctx = new MyEntities ();

                var qry = ctx.MyTable.Where(q => q.workorder == "100001076").OrderBy(q => q.id);
                dataGridView1.DataSource = qry;
                int i = 0;
                foreach (var item in qry)
                {
                    i++;
                    item.jobseqno = i.ToString();
                    ctx.SaveChanges();

                }

        }

How to update my whole table. i give new number collection jobseqno. But Error occured:

EntityException was unhandled

An error occurred while starting a transaction on the provider connection. See the inner exception for details:

Exception : New transaction is not allowed because there are other threads running in the session

Upvotes: 1

Views: 983

Answers (1)

alex
alex

Reputation: 3720

Looking over this blog post, the solution seems to be to store the elements you get from MyTable into an array, as opposed to working with the IEnumerable result directly. Try this:

var qry =
   ctx.MyTable
       .Where(q => q.workorder == "100001076")
       .OrderBy(q => q.id)
       .ToArray<MyTable>; // save it as a local array

Upvotes: 2

Related Questions