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 8.0 fails choosing the right constructor when creating entities from DB having multiple constructors #34783

Closed
SetTrend opened this issue Sep 28, 2024 · 1 comment

Comments

@SetTrend
Copy link

SetTrend commented Sep 28, 2024

EF Core 8.0 Fails Choosing The Right Constructor When Creating Entities From DB Having Multiple Constructors

In my test case, the Model.Lookups.ProjectItem entity provides three different constructors, each validating their constructor arguments:

public ProjectItem(int id, bool isRequired, ItemDataType dataType);
public ProjectItem(int id, bool isRequired, ItemDataType dataType, byte precision);
public ProjectItem(int id, bool isRequired, ItemDataType dataType, KnowledgeCategory knowledgeCategory);

Current Situation

When reading data from the database, EF Core 8.0 does not use the proper constructor.

With this data:

Id IsRequired DataType Precision KnowledgeCategory
3 0 6 0

… EF Core 8.0 fails to select constructor #3. It, instead, chooses constructor #1.

Desired Situation

EF Core should select the right constructor from …

  1. Number of non-nullable constructor parameters matching number of non-null column values,
    ordered by number of constructor parameters, descending
  2. parameter data type
  3. parameter name (if parameter data type alone cannot be uniquely matched)

If a row has more non-null columns than constructor parameters available, these surplus values ought to be set using property initializers then.

Minimum Reproducible Example

https://github.com/SetTrend/EFC-Constructor-MRE

@maumar
Copy link
Contributor

maumar commented Oct 4, 2024

Currently all constructor binding is static and non-configurable. Improvements to that area are tracked here: #10789

Also some documentation on what's currently possible: https://learn.microsoft.com/en-us/ef/core/modeling/constructors

@maumar maumar closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 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