Skip to content
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

PropertyEntry.CurrentValue should throw an exception #7920

Closed
sjb-sjb opened this issue Mar 17, 2017 · 5 comments
Closed

PropertyEntry.CurrentValue should throw an exception #7920

sjb-sjb opened this issue Mar 17, 2017 · 5 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@sjb-sjb
Copy link

sjb-sjb commented Mar 17, 2017

When the value assigned to propertyEntry.CurrentValue cannot be assigned into the database field, then the CurrentValue is not updated, but also no exception is thrown. This makes it harder to debug failed assignments. An exception should be thrown by CurrentValue (and, I suppose, OriginalValue) if the value cannot be assigned.

An example of this is a database table created with an entity property of type bool, which in the CLR was then changed to bool?. However no migration or change was made to the table. When the database was reopened, the bool values were assigned into the bool? CLR property. The propertyEntry CLR type was bool?, but propertyEntry.IsNullable was false. Then I tried to assign null to propertyEntry.CurrentValue. It did not do the assignment (value of CurrentValue was unchanged), but it also did not throw an exception.

@ajcvickers
Copy link
Member

@sjb-sjb The correct behavior (per current design) for this case would be that setting the CurrentValue should succeed and the value should be set to null. I have verified that this does not happen--the value does not get set to null. This is a bug we will fix.

That being said, CurrentValue should not throw in this case. In general, setting the CurrentValue should succeed any time that the property value is compatible with the CLR type of the property. One example of where this is important is store generated values where the CLR type may be nullable to allow null sentinels that indicate that the value to be used must be obtained from the store. In this case, the CLR type is bool?, null values are allowed in the entity, but the property can be marked as Required (IsNullable false) and no null value will be stored in the database.

@ajcvickers ajcvickers self-assigned this Mar 17, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 17, 2017
@sjb-sjb
Copy link
Author

sjb-sjb commented Mar 19, 2017 via email

@ajcvickers
Copy link
Member

@sjb-sjb [Required] will make the column non-nullable in the store.

@sjb-sjb
Copy link
Author

sjb-sjb commented Mar 21, 2017 via email

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
ajcvickers added a commit that referenced this issue Jun 26, 2017
Issue #7920

Previously we were not setting the property to null if it was marked as required--we were instead setting conceptual null. But when the CLR type is nullable, we don't need conceptual nulls, so just set the property to null.
ajcvickers added a commit that referenced this issue Jun 26, 2017
Issue #7920

Previously we were not setting the property to null if it was marked as required--we were instead setting conceptual null. But when the CLR type is nullable, we don't need conceptual nulls, so just set the property to null.
ajcvickers added a commit that referenced this issue Jun 27, 2017
Issue #7920

Previously we were not setting the property to null if it was marked as required--we were instead setting conceptual null. But when the CLR type is nullable, we don't need conceptual nulls, so just set the property to null.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 27, 2017
@sjb-sjb
Copy link
Author

sjb-sjb commented Oct 26, 2017

Thanks for the fix!

Just a comment on a potential 'gotcha' ... if you have a non-nullable CLR property, say of type bool, and you try to assign null to it using

 propertyInfo.SetValue( this, null)

then it will assign the default value in the CLR property instead of throwing an exception. All perfectly legitimate and it is documented that way in PropertyInfo.SetValue.

Because of this, if you have a Required property of type bool? and you change it to bool, you may not get the same results even though the table property was bool all along.

Not really an EntityFrameworkCore problem at all, it's just one of those things that can creep out and bite you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants