Using EF Core 1.1
and Asp Core 1.1
on Mac
With a model like
public class Model {
public int? Id { get; set; }
public string OtherProp { get; set; }
}
And an action
public class ModelController
{
public Model Get(int? id)
{
DbContext.Set<Model>.Find(id)
}
}
Accessing the url /model/10
fails with a message The key value at position 0 of the call to 'DbSet<Model>.Find' was of type 'int', which does not match the property type of 'Nullable<int>'
The error message is clear but my question is if there is a way to make this work, it seems like a very common use case and one that used to work in previous versions.
I tried casting id
to int?
but it didn't work. Any ideas?
Also, in case it's useful this is the line that's breaking in EF
Looking at the code you've already linked: this cannot work, since the CLR gives you always the underlying type for a null-able instance. (Don't know why, had similar issues before...)
In code: typeof(int?) != ((int?)1).GetType()
.
Thus, comparing the type of the property (int?
) and the type of the argument will always fail. You have to ask the EF Core team to add support for null-able types for that.