Possible Null assignment to non-nullable entity

Completed

I've a strange warning on the following block of code :

public static int FooBar(object content)
{
   var contentType = _db.Model.FindEntityType(content.GetType());
   if (contentType?.GetTableName() == null) return 0;
   var storeObjectIdentifier = StoreObjectIdentifier.Table(contentType.GetTableName(), contentType.GetSchema());
return 0;
}

If I understand well the warning, he complaints because the first parameter does not allow null

(public static StoreObjectIdentifier Table(string name, string? schema = null)

But if its null I've already left the block, no ?

Thanks

2 comments
Comment actions Permalink

I suspect R# can't be certain that calling the same method twice will return the same value. Try storing the value returned from GetTableName in a local variable instead.

var contentType = _db.Model.FindEntityType(content.GetType());
string tableName = contentType?.GetTableName();
if (tableName == null) return 0;
var storeObjectIdentifier = StoreObjectIdentifier.Table(tableName, contentType.GetSchema());
0
Comment actions Permalink

It makes sense, thank you.

0

Please sign in to leave a comment.