ช่วงนี้มีไปช่วยน้องในทีมแก้ปัญหาระบบซื้อขาย Bond โดยน้องเอา Service เดิมที่เคยทำไว้ในผูกกันเป็น Flow ใหม่ครับ แล้วทีนี้เจอ Error
Exception has occurred: CLR/System.InvalidOperationException An exception of type 'System.Invalid0perationException' occurred in System.Data.Sqlclient.d1l but was not handled in user code: 'requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.' at System.Data.Sqlclient.Sqlcommand.ValidateCommand(Boolean async, String method) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,TaskCompletionSource'1 completion, Int32 timeout,sk task, Boolean asyncWrite, String method) at System.Data.Sqlclient.SqlCommand.ExecuteReader(CommandBehavior behavior) at Dapper.SqIMapper.ExecuteReaderWithFlagsFallback(IDbCommandcmd,Boolean wasclosed,CommandBehaviorbehavior) in /_/Dapper/SqIMapper.cs:1ine 1066 at Dapper.SqlMapper.<QueryImpl>d_140*1.MoveNext() in /_/Dapper/SqlMapper.cs:line 1083 at System.Collections.Generic.List 1..ctor(Inumerable'1 collection) at System. Ling. Enumerable. ToList[TSource] (IEnumerable'1 source)
หลังจากลองไล่ดู Code พบว่าเป็นส่วนคำสั่งที่เรียก Dapper ให้มันช่วย Query ให้ เลยเพิ่มตัว Transaction ใส่ครอบเข้าไปครับ
public IList<InvestTxDTO> FindBuyUnit(int pPortfolioId, int pSecurityId, DateTime pTradeDate, String pRefAction) { IList<int> investTxTypeIdLs = new List<int>() { INVESTTXTYPE_CONST.BUY, INVESTTXTYPE_CONST.RIGHT, INVESTTXTYPE_CONST.DEPOSIT , INVESTTXTYPE_CONST.TRANSFERIN, INVESTTXTYPE_CONST.SPLIT, INVESTTXTYPE_CONST.INCREASE , INVESTTXTYPE_CONST.TRANSFERTO, INVESTTXTYPE_CONST.ADJUSTED, INVESTTXTYPE_CONST.LOADIN }; var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() }; pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.PortfolioId, Operator.Eq, pPortfolioId)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.SecurityId, Operator.Eq, pSecurityId)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.TradeDate, Operator.Le, pTradeDate)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.RefAction, Operator.Eq, pRefAction)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.IsPosted, Operator.Eq, CONFIRM_CODE.YES)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.IsCancelled, Operator.Eq, CONFIRM_CODE.NO)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.IsDeleted, Operator.Eq, CONFIRM_CODE.NO)); pg.Predicates.Add(Predicates.Field<InvestTxDTO>(f => f.InvestTxTypeId, Operator.Eq, investTxTypeIdLs)); IEnumerable<InvestTxDTO> ls = connection.GetList<InvestTxDTO>(pg, transaction: Transaction); //<<<-- Add Transaction Scope return ls.ToList<InvestTxDTO>(); }
อีกเคส ต้องไปใส่ Transaction Scope ด้วย พวก BeginTran / Commit
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.