I'm trying to get rid of Code Contract warnings for Entity Framework classes and methods by using
[assembly: SuppressMessage("Microsoft.Contracts", "Requires", Target = "System.Data.Entity")]
[assembly: SuppressMessage("Microsoft.Contracts", "NonNull", Target = "System.Data.Entity")]
Now I have the following method:
public static void TestMethod(this DbContext context, EntityBaseClass entity)
{
Contract.Requires(context != null);
var tmp = context.ChangeTracker.Entries<EntityBaseClass>();
// do something
}
This throws a warning:
Code Contracts: Missing precondition in an externally visible method. Consider adding Contract.Requires(((System.Data.Entity.DbContext)context).ChangeTracker != null); for parameter validation.
Obviously I could just do what the warning tells me to do but since ChangeTracker
will never be null
, it would be redundant. That's why I added the SuppressMessage
statements shown above to my AssemblyInfo.cs. The suppressing works in a lot of other cases but in this one it fails and I can't figure out why.