EF 不允许启动新事务,因为有其他线程正在该会话中运行。

为什么80%的码农都做不了架构师?>>>   hot3.png

引起原因:在查询中提交了更改。如在遍历的时候,调用了savechanges();

解决:把savechange()提到循环外.          

 

IOrderedQueryable tb = ent.TOHOSPITAL_TBL.Where(record =>
                    (record.TOHOSPITAL_STATE_TYPE_TBL.VALUE == (int)ToHospitolState.treatment) ||
                    (record.TOHOSPITAL_STATE_TYPE_TBL.VALUE == (int)ToHospitolState.Clinic)).
                        OrderByDescending(record => record.TOHOSPITAL_TIME);
                foreach (TOHOSPITAL_TBL tohospotal in tb)
                {
                    if (!tohospotal.TOHOSPITAL_STATE_TYPE_TBLReference.IsLoaded)
                    {
                        tohospotal.TOHOSPITAL_STATE_TYPE_TBLReference.Load();
                    }
                    ChangeState(tohospotal);
               ent.SaveChanges();
                }
原因IOrderedQueryable tb还在查询中,调用ent.SaveChanges();产生的。

ent.SaveChanges();提出即可

代码:

                IOrderedQueryable tb = ent.TOHOSPITAL_TBL.Where(record =>
                    (record.TOHOSPITAL_STATE_TYPE_TBL.VALUE == (int)ToHospitolState.treatment) ||
                    (record.TOHOSPITAL_STATE_TYPE_TBL.VALUE == (int)ToHospitolState.Clinic)).
                        OrderByDescending(record => record.TOHOSPITAL_TIME);
                foreach (TOHOSPITAL_TBL tohospotal in tb)
                {
                    if (!tohospotal.TOHOSPITAL_STATE_TYPE_TBLReference.IsLoaded)
                    {
                        tohospotal.TOHOSPITAL_STATE_TYPE_TBLReference.Load();
                    }
                    ChangeState(tohospotal);
                }
                ent.SaveChanges();

转载于:https://my.oschina.net/wzzz/blog/182303

你可能感兴趣的:(EF 不允许启动新事务,因为有其他线程正在该会话中运行。)