-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
ef core ignores parameterized constructor if parameterless constructor exists #27466
Comments
Duplicate of dotnet/EntityFramework.Docs#2571 |
Well, it depends on point of view.. is it inconsistency in a document or bug in a code. |
@ish-1313 It is by-design. We made it this way to avoid breaking existing applications with multiple constructors. |
pity. i was planning to use constructor with parameters to get rid of unnecessary object initialization when it is loaded from db. no plans to change it in the future versions with data annotation or fluent api ? |
thank you, will follow 10789. |
Does this remain true? will EFCore use the parameterless constructor even when a parameterized one exists?. |
@cdrfenix Yes. |
File a bug
documentation for ef core here https://docs.microsoft.com/en-us/ef/core/modeling/constructors states that
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
in reality if entity has parameterless constructor it will be called even if entity has parameterized one with correct properties.
constructor with parameter is not called until i change parameterless to something like
then ef core starts to call constructor with (int ID)
EF Core version: 6.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Windows 20H2 (19042.1526)
Microsoft Visual Studio Professional 2022
Version 17.1.0
VisualStudio.17.Release/17.1.0+32210.238
The text was updated successfully, but these errors were encountered: