CA1001的Visual Studio 2012代码分析警告。这是什么意思? [英] CA1001 Visual Studio 2012 Code Analysis warning. What does it mean?
问题描述
这并不重要,但我想弄清楚它是什么,告诉我,这是一个合法的警告?有人可以解释简单来说这个错误给我吗?
CA1001 类型的自己支配领域应该是一次性的。
在'MemVoteManager实现IDisposable,因为它创建
的成员如下IDisposable的类型:CongressDBEntities。如果MemVoteManager'
以前发货,并称实现IDisposable
这种类型被认为是现有的
消费者带来了重大更改新成员。
块引用>
公共类MemVoteManager:AbstractDataManager,IMemVoteManager
{
私人CongressDBEntities上下文=新CongressDBEntities();
公众诠释AddMemVote(tMemVoteScore MVS)
{
//插入模型
context.tMemVoteScores.Add(MVS);
context.SaveChanges();
INT newPK = mvs.MemVoteScoresID;
//与PK更新时髦列ID以及
VAR memVoteItem =(在context.tMemVoteScores
从m其中m.MemVoteScoresID == newPK
选择M) .SingleOrDefault();
memVoteItem.ID = memVoteItem.MemVoteScoresID;
context.SaveChanges();
返回newPK;
}
解决方案您的可能的实施
的IDisposable
这样的背景下将被处置时,消费者与您的类来完成的,但你可能会关闭没有上下文是类的成员更好。只要创建它,当你需要它,当你完成它的配置:公众诠释AddMemVote(tMemVoteScore MVS)
{
//插入模型使用
(CongressDBEntities背景=新CongressDBEntities())
{
context.tMemVoteScores.Add(MVS);
context.SaveChanges();
INT newPK = mvs.MemVoteScoresID;
//与PK更新时髦列ID以及
VAR memVoteItem =(在context.tMemVoteScores
从m其中m.MemVoteScoresID == newPK
选择M) .SingleOrDefault();
memVoteItem.ID = memVoteItem.MemVoteScoresID;
context.SaveChanges();
}
返回newPK;
}
上下文是轻量级所以没有,每次他们创造了巨大的损失。另外,您再不必担心消费者通知您处置的背景下,和你没有大量的内存内置了变化,如果这个类的一个实例被多次使用。
It is not that important but I am trying to figure out what it is telling me and is it a legitimate warning ? Can someone explain this error in simple terms for me ?
CA1001 Types that own disposable fields should be disposable
Implement IDisposable on 'MemVoteManager' because it creates members of the following IDisposable types: 'CongressDBEntities'. If 'MemVoteManager' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers.
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; }
解决方案You could implement
IDisposable
so the context will be disposed of when consumers are done with your class, but you may be better off NOT having the context be a member of the class. Just create it when you need it and dispose of it when you're done: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; }
Contexts are lightweight so there's not a huge penalty for creating them each time. Plus you then don't have to worry about consumers notifying you to dispose of the context, and you don't have a lot of built-up changes in memory if one instance of the class is used many times.
这篇关于CA1001的Visual Studio 2012代码分析警告。这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!