I have written this snippet and its contract:
[Pure]
public string getLevelName()
{
using (var c = new myContext())
{
Contract.Ensures(Contract.Result<string>() == c.Level.FirstOrDefault(i => i.levelId == this.levelId).name);
return c.Level.FirstOrDefault(i => i.levelId == this.levelId).name;
}
}
but I get this error for contract:
Contract section within try block.
How can I avoid this error for this sort of contract for a method?
Try calling another method,
public string getLevelName()
{
using (var c = new myContext())
{
return getLevelNameFrom(c);
}
}
public string getLevelNameFrom(MyContext c)
{
Contract.Ensures(Contract.Result<string>() == c.Level.FirstOrDefault(i => i.levelId == this.levelId).name);
return c.Level.FirstOrDefault(i => i.levelId == this.levelId).name;
}