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

Make more clear that parameterless constructors have priority #2571

Open
rathga opened this issue Aug 15, 2020 · 2 comments
Open

Make more clear that parameterless constructors have priority #2571

rathga opened this issue Aug 15, 2020 · 2 comments

Comments

@rathga
Copy link

rathga commented Aug 15, 2020

From the page:

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.

This suggests that parameterized constructors have priority over default parameterless in the case where both exist. My understanding from the comments on #10789 is the opposite:

@xKloc Starting with preview2, if there are both parameterized and empty constructors, then EF will choose the empty one. See #10852

Originally posted by @ajcvickers in dotnet/efcore#10789 (comment)

I suggest the document wording be revised to make it clearer. It has taken me some digging today to clarify it.

My domain model has parameterised constructors that trigger domain events, so it is important EF uses the protected parameterless ones.


Document Details

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

@aunikitin
Copy link

aunikitin commented Dec 17, 2023

Guys, 4 years from this. Common, is it so difficult to edit doc?

How to prioritize constructor for EF Core? I need some initialization logic in constructor and want it to be clear for users which one is for public and which is for EF Core

I tried something like this, but EF Core always choose constuctor with lower amount of parameters. But from doc it should use the 1st one.
image

@aunikitin
Copy link

aunikitin commented Dec 18, 2023

Afterwards, we with team created really dirty hack to bypass this behaviour. We just renamed payload to body, to force EF Core not to use constructor for package consumers

@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

3 participants