1

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.

Here are my options: enter image description here

Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
  • in the Visual Studio Code Contracts property pane. just uncheck the "Perfom Runtime Contract Checking" – Bassam Alugili Jul 11 '16 at 13:58
  • It is, I'm talking about static checking. But your comment reminds me that I wanted to add my Coder Contract options to the question. – Tim Pohlmann Jul 11 '16 at 13:59
  • @BassamAlugili I want static checking though. I just want the checker to ignore all warning resulting from using Entity Framework. – Tim Pohlmann Jul 11 '16 at 14:03
  • Have you tried suppressing messages for the target `System.Data.Entity.Infrastructure`, which is the actual namespace where `DbChangeTracker` resides? Just as a wild shot – Jcl Jul 11 '16 at 14:03
  • @Jcl I did and it did not help. I think the SuppressMessage attribute is supposed to work recursively for children namespaces as well. – Tim Pohlmann Jul 11 '16 at 14:13
  • Uhm, I'm just wild-shotting here... but shouldn't the target of the message suppression be the namespace *where your `TestMethod` is*, and not the one where the null/required objects are? – Jcl Jul 11 '16 at 14:16
  • @Jcl That would completely defeat the point. Also according to this answer it should be possible this way: http://stackoverflow.com/a/29304326/4961688 – Tim Pohlmann Jul 11 '16 at 14:17
  • @TimPohlmann I actually think that answers says what I said (although I understand how that doesn't help your problem): that `Target` makes it suppress the message on a specific namespace or method... but the message is happening on your `TestMethod`, not on `System.Data.Entity`. Again: I understand how it doesn't help your problem, but reading the web (and that answer) seems that's how it works – Jcl Jul 11 '16 at 14:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116997/discussion-between-tim-pohlmann-and-jcl). – Tim Pohlmann Jul 11 '16 at 14:22

0 Answers0