c# – CA1001 Visual Studio 2012代码分析警告.这是什么意思?
发布时间:2020-12-15 04:23:33 所属栏目:百科 来源:网络整理
导读:它并不重要,但我想弄清楚它告诉我什么,这是一个合理的警告吗?有人可以用简单的术语解释这个错误吗? CA1001 Types that own disposable fields should be disposable Implement IDisposable on ‘MemVoteManager’ because it creates members of the follo
|
它并不重要,但我想弄清楚它告诉我什么,这是一个合理的警告吗?有人可以用简单的术语解释这个错误吗?
public class MemVoteManager : AbstractDataManager,IMemVoteManager
{
private CongressDBEntities context = new CongressDBEntities();
public int AddMemVote(tMemVoteScore mvs)
{
//Insert Model
context.tMemVoteScores.Add(mvs);
context.SaveChanges();
int newPK = mvs.MemVoteScoresID;
//Update funky column ID with PK as well
var memVoteItem = (from m in context.tMemVoteScores
where m.MemVoteScoresID == newPK
select m).SingleOrDefault();
memVoteItem.ID = memVoteItem.MemVoteScoresID;
context.SaveChanges();
return newPK;
}
解决方法
您可以实现IDisposable,以便在消费者完成您的课程时处理上下文,但您可能最好不要让上下文成为该类的成员.只需在需要时创建它并在完成后将其丢弃:
public int AddMemVote(tMemVoteScore mvs)
{
//Insert Model
using(CongressDBEntities context = new CongressDBEntities())
{
context.tMemVoteScores.Add(mvs);
context.SaveChanges();
int newPK = mvs.MemVoteScoresID;
//Update funky column ID with PK as well
var memVoteItem = (from m in context.tMemVoteScores
where m.MemVoteScoresID == newPK
select m).SingleOrDefault();
memVoteItem.ID = memVoteItem.MemVoteScoresID;
context.SaveChanges();
}
return newPK;
}
上下文是轻量级的,因此每次创建它们都没有太大的代价.此外,您不必担心消费者会通知您处理上下文,并且如果多次使用该类的一个实例,则内存中没有很多内置更改. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
