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

GetProperty should return null if not found; instead it throws an exception #27791

Closed
VAchris opened this issue Apr 8, 2022 · 3 comments · Fixed by #27942
Closed

GetProperty should return null if not found; instead it throws an exception #27791

VAchris opened this issue Apr 8, 2022 · 3 comments · Fixed by #27942
Labels
area-api-docs area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@VAchris
Copy link

VAchris commented Apr 8, 2022

We have a disconnect between what the documents say should be happening versus what's implemented.

In the file IReadOnlyEntityType.cs when you try to get a property that does not exist it throws an InvalidOperationException whereas the expected return value is null.

Example

var entityType = MyDbContext.Set<E>().EntityType;
var property = entityType.GetProperty(invalidPropertyName); // this throws an exception

Include stack traces

System.InvalidOperationException: The property 'MyTable.invalidPropertyName' could not be found. Ensure that the property exists and has been included in the model.
   at Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityType.GetProperty(String name)
   at Microsoft.EntityFrameworkCore.Metadata.IEntityType.GetProperty(String name)

Include provider and version information

EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6
Operating system: Windows 10
IDE: VS 2022

@stevendarby
Copy link
Contributor

Just wanted to share a workaround I've used in the past which is to use FindProperty instead. Not sure what the semantic difference between Get and Find is but the latter method allows null.

@maumar
Copy link
Contributor

maumar commented Apr 9, 2022

FindProperty is the correct way to do it. GetProperty calls FindProperty under the covers and throws exception if the result is null. We should update the xml docs for GetProperty, as it indicates that null can be returned from GetProperty, which is wrong.

@KurogamiLight3303
Copy link

I think the exception case not must be workarounded, its safe to know that the problem exist that have a an error in the program logic and dont know why. Just my opinion

@ajcvickers ajcvickers added this to the 7.0.0 milestone Apr 23, 2022
@ajcvickers ajcvickers self-assigned this Apr 23, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 3, 2022
AndriySvyryd added a commit that referenced this issue May 3, 2022
Use client value comparer as provider value comparer if possible
The client value comparer will not be used if the model type is a nullable value type
Fix some API issues
Remove some unused code

Fixes #27738
Fixes #27850
Fixes #27791
Part of #27588
AndriySvyryd added a commit that referenced this issue May 3, 2022
Use client value comparer as provider value comparer if possible
The client value comparer will not be used if the model type is a nullable value type
Fix some API issues
Remove some unused code

Fixes #27738
Fixes #27850
Fixes #27791
Part of #27588
AndriySvyryd added a commit that referenced this issue May 5, 2022
Use client value comparer as provider value comparer if possible
    The client value comparer will not be used if the model type is a nullable value type since we need the types to match exactly and the provider type is never nullable
Fix some API issues
Remove some unused code

Fixes #27738
Fixes #27850
Fixes #27791
Part of #27588
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview5 May 25, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview5, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-api-docs area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants