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.
It's letting you know that field context
contains disposable members. That means those members need to have Dispose()
called on them so that Garbage Collection can occur. 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.
So modify you code as such:
public class MemVoteManager : AbstractDataManager, IMemVoteManager, IDisposable
and then implement the members of the IDisposable
interface like this:
public void Dispose()
{
// call dispose on the context and any of its members here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With