开发者

Disposing DataContext and code analysis messages

开发者 https://www.devze.com 2023-03-20 01:35 出处:网络
I just ran code analysis on one of our projects and was a bit surprised when I received the following warning message;

I just ran code analysis on one of our projects and was a bit surprised when I received the following warning message;

CA2000 : Microsoft.Reliability : In method 'DataImport.ImportSalesExecBackFill(Stream)', call System.IDisposable.Dispose on object 'dc' before all references to it are out of scope.

in reference to the following peice of code;

            using (DataDataContext dc = new DataDataContext(ConfigurationManager.ConnectionStrings["data"].ConnectionString))
            {
                List<tableSale> sample = (from s in dc.tableSales
                                                   where bfData.Select(d => d.RegNo).ToList().Contains(s.RegNum)
                                                   select s).ToList();

                var matches = (from s in sample
                               join bfd in bfData on s.RegNum equals bfd.RegNo
                               where string.IsNullOrEmpty(s.OpDesc)
                               select new { UserId = s.UserID, OpId = String.Format("SMMT{0}-{1}", s.DlrNo, bfd.OpName), OpDesc = bfd.OpName }).ToList();

                List<tableSale> updQuery = (from s in dc.tableSales
                                                     where matches.Select(d => d.UserId).ToList().Contains(s.UserID)
                                                     select s).ToList();

                foreach (tableSale rec in updQuery)
                {
                    rec.OpID = matches.Where(s => s.UserId == rec.U开发者_StackOverflow中文版serID).Select(s => s.OpId).First();
                    rec.OpDesc = matches.Where(s => s.UserId == rec.UserID).Select(s => s.OpDesc).First();
                }

                dc.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);

                using (PCSalesDataContext pcdc = new PCSalesDataContext(ConfigurationManager.ConnectionStrings["LatData"].ConnectionString))
                {
                    List<tblSale> upd700 = (from s in pcdc.tblSales
                                            where matches.Select(d => d.UserId).ToList().Contains(s.UserID)
                                            select s).ToList();

                    foreach (tblSale rec in upd700)
                    {
                        rec.OpID = matches.Where(s => s.UserId == rec.UserID).Select(s => s.OpId).First();
                        rec.OpDesc = matches.Where(s => s.UserId == rec.UserID).Select(s => s.OpDesc).First();
                    }

                    pcdc.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
                }

                result = true;
            }   

Now I was under the impression that the datacontext will always be disposed at the end of the scope of the using statement. Am I missing something or is this a warning message that I can happily ignore?


You probably have too many function calls (more than 63) inside the using statement. See

http://connect.microsoft.com/VisualStudio/feedback/details/557088/vb-net-using-block-ca2000-warning-if-too-many-calls

and

https://connect.microsoft.com/VisualStudio/feedback/details/607263/warning-ca2000-although-object-is-disposed

So, yes, it's a bug in code analysis and you can ignore the message, but you may want to refactor your code anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消