-
-
Notifications
You must be signed in to change notification settings - Fork 158
SetHasOneForeignKeyValue detects string as non-nullable type and throws exception #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for pointing this out! In which scenario are you encountering this issue? I'll need a little reproduction case to be able to address this issue further. You could make a PR with a test that exposes this bug or share a repo with an example app that exposes the bug. |
Thanks for a quick reply @maurei !
I think adding string as a special case might be enough as I can't think what else anyone could use that is not int?, guid? or string.
|
Just wondering: since you're using FKs of type |
Allowing string foreign keys on optional HasOne relationship
Hi @maurei - the PR is there with a new test which should hopefully explain the scenario better.
|
Okay I was wondering whether you were doing some bad practise with respect to your primary keys but this seems like a totally valid usecase! I will review your PR shortly. |
We'll release this in alpha4 which I'm expecting be published by the end of next week |
Description
I have a 0..1 to 1 relationship using foreign key of type string. As string can be null so it should be fine to use it as a foreign key in this scenario but I'm getting an exception thrown from
SetHasOneForeignKeyValue
method:"Cannot set required relationship identifier '{hasOneAttr.IdentifiablePropertyName}' to null because it is a non-nullable type."
I believe this is because the method uses this code for the check:
Nullable.GetUnderlyingType(foreignKeyProperty.PropertyType)
which returns null for type string.
That is because GetUnderlyingType only works as expected here for types derived from Nullable which string is not.
https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,132
...
Environment
The text was updated successfully, but these errors were encountered: