CA1001 Visual Studio 2012代码分析警告。 这是什么意思?

它并不重要,但我想弄清楚它告诉我什么,这是一个合理的警告吗? 有人可以用简单的语言解释这个错误吗?

CA1001拥有一次性领域的类型应该是一次性的

在’MemVoteManager’上实现IDisposable,因为它创建了以下IDisposable类型的成员:’CongressDBEntities’。 如果先前发布了“MemVoteManager”,则将实现IDisposable的新成员添加到此类型将被视为对现有使用者的重大更改。

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; } 

上下文是轻量级的,因此每次创建它们都没有太大的代价。 此外,您不必担心消费者会通知您处理上下文,并且如果多次使用该类的一个实例,则内存中没有大量的内置更改。

它让你知道现场context包含一次性成员。 这意味着那些成员需要调用Dispose() ,以便可以进行垃圾收集。 因此,它希望您在MemVoteManager上实现接口IDisposable ,以便您可以在上下文和/或其一次性成员上调用Dispose()

所以修改你的代码:

 public class MemVoteManager : AbstractDataManager, IMemVoteManager, IDisposable 

然后实现IDisposable接口的成员,如下所示:

 public void Dispose() { // call dispose on the context and any of its members here }