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

ef core ignores parameterized constructor if parameterless constructor exists #27466

Closed
ish-1313 opened this issue Feb 17, 2022 · 8 comments
Closed

Comments

@ish-1313
Copy link

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.

       public cInventoryStock(int ID)
        {
            this.ID = ID;
            Debug.WriteLine("constructor without init");
        }

        public cInventoryStock()
        {
            base.Init();
            Debug.WriteLine("constructor with init");
        }

constructor with parameter is not called until i change parameterless to something like

        public cInventoryStock(bool state = false)
        {
            base.Init();
            Debug.WriteLine("constructor with init");
        }

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

@ajcvickers
Copy link
Contributor

Duplicate of dotnet/EntityFramework.Docs#2571

@ish-1313
Copy link
Author

Duplicate of dotnet/EntityFramework.Docs#2571

Well, it depends on point of view.. is it inconsistency in a document or bug in a code.

@ajcvickers
Copy link
Contributor

@ish-1313 It is by-design. We made it this way to avoid breaking existing applications with multiple constructors.

@ish-1313
Copy link
Author

@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.
(or to skip propertychanged notification code).

no plans to change it in the future versions with data annotation or fluent api ?

@ajcvickers
Copy link
Contributor

@ish-1313 Configuration of this is tracked by #10789. It is already possible to bind explicitly to a different constructor by dropping down to lower level code. I can come up with an example if it would help.

@ish-1313
Copy link
Author

@ish-1313 Configuration of this is tracked by #10789. It is already possible to bind explicitly to a different constructor by dropping down to lower level code. I can come up with an example if it would help.

thank you, will follow 10789.
as for low level code example. no, thank you, i am ok with constructor with fake parameter. at least for now.. i just want to generate guid and few time stamps. and don't want to ovveride on savechanges, use fulentapi, or other "standard methods". idea to use different constructors seems much better even now :) thank you

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@rofenix2
Copy link

Does this remain true? will EFCore use the parameterless constructor even when a parameterized one exists?.

@ajcvickers
Copy link
Contributor

@cdrfenix Yes.

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