简体   繁体   English

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

[英]CA1001 Visual Studio 2012 Code Analysis warning. What does it mean?

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 CA1001拥有一次性领域的类型应该是一次性的

Implement IDisposable on 'MemVoteManager' because it creates members of the following IDisposable types: 'CongressDBEntities'. 在'MemVoteManager'上实现IDisposable,因为它创建了以下IDisposable类型的成员:'CongressDBEntities'。 If 'MemVoteManager' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers. 如果先前发布了“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;
    }

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. 可以实现IDisposable以便在消费者完成您的课程时处理上下文,但您可能最好不要让上下文成为该类的成员。 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. 此外,您不必担心消费者会通知您处理上下文,并且如果多次使用该类的一个实例,则内存中没有很多内置更改。

It's letting you know that field context contains disposable members. 它让你知道现场context包含一次性成员。 That means those members need to have Dispose() called on them so that Garbage Collection can occur. 这意味着那些成员需要调用Dispose() ,以便可以进行垃圾收集。 Therefore, it wants you to implement the interface IDisposable on MemVoteManager so that you can call Dispose() on the context and/or its members that are disposable. 因此,它希望您在MemVoteManager上实现接口IDisposable ,以便您可以在上下文和/或其一次性成员上调用Dispose()

So modify you code as such: 所以修改你的代码:

public class MemVoteManager : AbstractDataManager, IMemVoteManager, IDisposable

and then implement the members of the IDisposable interface like this: 然后实现IDisposable接口的成员,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM