处理DataContext和代码分析消息
本文关键字:消息 代码 DataContext 处理 | 更新日期: 2023-09-27 18:01:58
我刚刚对我们的一个项目进行了代码分析,当我收到以下警告消息时,我有点惊讶;
CA2000: Microsoft。可靠性:在方法'DataImport.ImportSalesExecBackFill(Stream)'中,在对象'dc'的所有引用超出范围之前调用System.IDisposable.Dispose。
引用下面的代码段;
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.UserID).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;
}
现在我的印象是数据上下文总是在using语句的作用域结束时处理。是我错过了什么,还是这是一个我可以愉快地忽略的警告信息?
您可能在using
语句中有太多的函数调用(超过63)。看到
和
https://connect.microsoft.com/VisualStudio/feedback/details/607263/warning-ca2000-although-object-is-disposed所以,是的,这是代码分析中的一个错误,您可以忽略该消息,但您可能无论如何都想重构您的代码。