.NET 4 Code Contracts: "requires unproven: source! = Null"

I just started using code contracts in my project. However, I had a problem with my repository implementation, which queries my database using the Entity Framework.

I have the following method:

public IEnumerable<Organization> GetAllOrganizations()
{
    return _uow.CreateSet<Party>().OfType<Organization>().AsEnumerable();
}

The method returns a collection containing all organizations in the database, or an empty collection in which there are no organizations in the database.

However, this is not the case, according to CodeContracts, which give me an error: "requires unproven: source! = Null"

What is he trying to tell me? I can satisfy code contracts using Contract.Assume, assuming it will always find something, but then I need to do this in all methods that read data from the database.

Am I missing something, or is this the intended behavior when you work with databases and LINQ?

+5
source share
1 answer

I assume that one of the CreateSet, OfType, and AsEnumerable methods is declared as an extension method with this parameter called "source", and CodeContrcts cannot prove that it is not null. Also, you do not need to add a Requires clause to indicate that _uow does not matter for input?

CreateSet seems to be an extension method because it does not appear on MSDN. If a method should never return null, you can enforce it by adding this contract to CreateSet:

Contract.Ensures(Contract.Result<T>() != null);

CodeContracts , , OfType , .

+2

All Articles