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

"Entity types with constructors" documentation doesn't explain how the framework prioritizes constructors for instantiation. #3636

Open
ErroneousFatality opened this issue Dec 20, 2021 · 2 comments

Comments

@ErroneousFatality
Copy link

ErroneousFatality commented Dec 20, 2021

"Entity types with constructors" documentation doesn't explain how the framework prioritizes constructors for instantiation.

The ambiguous text in question:

When EF Core creates instances of these types, such as for the results of a query, it will first call the default parameterless constructor and then set each property to the value from the database. However, if EF Core finds a parameterized constructor with parameter names and types that match those of mapped properties, then it will instead call the parameterized constructor with values for those properties and will not set each property explicitly.

Does the framework look for the constructor that has the greatest number of parameters matching properties?
Is it possible to define which constructor for a given entity should be used? Or to set the global method of choosing?

In my case, I've got a private default constructor I always leave for EF Core.
I have another internal constructor my domain services use to create the entity, and that constructor can have some heavy validation or other type of logic inside itself.
If I create another private constructor that accepts parameters for all the properties, will that one be prioritized over the one mentioned above?

What if I already have a constructor that accepts parameters for all properties and does some logic I wouldn't want to run on EFCore's entity instantiation?
How could I solve that, given that I can't create two constructors with the same parameter signature?

EDIT: I've tested it out on EFCore 6.0.1. The framework prioritizes the constructor with minimal parameter count (including 0) where all parameters correspond to a property.
If this was made clear in the documentation I wouldn't have had to waste time on testing it out!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ErroneousFatality ErroneousFatality changed the title "Entity types with constructors" documentation doesn't explain how prioritizes constructors to choose. "Entity types with constructors" documentation doesn't explain how the framework prioritizes constructors for instantiation. Dec 20, 2021
@ajcvickers
Copy link
Contributor

@ErroneousFatality This is related to #2571, #1847, and #746. I agree this documentation could be better.

EF always uses a parameterless constructor if one is present. This avoids breaking from earlier versions of EF that would only use parameterless constructors. Configuration for the constructor to use is tracked by dotnet/efcore#10789.

@ErroneousFatality
Copy link
Author

Thanks for the info.
The biggest issue I'm having, due to the way it currently works, is that when using the nullable reference type enabled configuration, I'd like to have a constructor for EF that covers all the non-nullable types that need to have values on construction, but often I can't because my "domain" constructor has equal or less parameters. So I end up with some monstrosity like
private Entity(string property1) {Property1 = property1; Property2 = default!; Property3 = default!;}

@ajcvickers ajcvickers self-assigned this Jan 13, 2022
@ajcvickers ajcvickers added this to the Backlog milestone Jan 13, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants