警告 CA1001 工具 IDisposable 可忽略不计

Is warning CA1001 implement IDisposable negligible

我正在使用 Repository 模式并拥有我的通用存储库 class

public class GeneralRepository<T> where T:class
{
    private Context con = new Context();
    protected DbSet<T> Dbset { get; set; }

    public GeneralRepository()
    {
        Dbset = con.Set<T>();
    }
    public IEnumerable<T> GetAll()
    {
        return Dbset;
    }
    public T SelectByID(int? id)
    {
        var rec = Dbset.Find(id);
        return rec;
    }
    public void AddRecord(T entity)
    {
        Dbset.Add(entity);
    }
    public void EditRecord(T entity)
    {
        con.Entry(entity).State = EntityState.Modified;
    }

    public void Detach(T entity)
    {
       con.Entry(entity).State = EntityState.Detached;
    }
    public void Save()
    {
        con.SaveChanges();
    }
}

有多个本机存储库继承了我的通用存储库,例如:

public class EmailAssignRepository:GeneralRepository<EmailAssign>
public class DepartmentRepository:GeneralRepository<Department>

在我的代码中 运行 一切都很好,但是当我 运行 代码分析时它给了我警告。这是什么意思?可以忽略不计吗?如果不是如何克服这个警告?

基于MSDN:

A class implements the IDisposable interface to dispose of unmanaged resources that it owns. An instance field that is an IDisposable type indicates that the field owns an unmanaged resource. A class that declares an IDisposable field indirectly owns an unmanaged resource and should implement the IDisposable interface. If the class does not directly own any unmanaged resources, it should not implement a finalizer.

原因:

A class declares and implements an instance field that is a System.IDisposable type and the class does not implement IDisposable.

如何解决违规问题

To fix a violation of this rule, implement IDisposable and from the System.IDisposable.Dispose method call the Dispose method of the field.

看看这个:How to implement IDisposable inheritance in a repository?


何时抑制警告(以及您的问题它可以忽略不计吗?否)

Do not suppress a warning from this rule.