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

Allow EF to call parameterized entity constructors #10703

Closed
codepb opened this issue Jan 13, 2018 · 5 comments
Closed

Allow EF to call parameterized entity constructors #10703

codepb opened this issue Jan 13, 2018 · 5 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@codepb
Copy link

codepb commented Jan 13, 2018

When trying to create classes with only readonly properties, you currently have to use properties with backing fields to be able to construct with Entity Framework, and it is required to have an empty constructor. It would be preferable if Entity Framework could construct the objects by passing the values to a constructor with a parameter of the same name (much like JSON.net can). This would allow truly readonly properties, and keep the class simpler.

Steps to reproduce

It would be preferable to support a class structured like the following:

public class SampleClass
{
    public string Prop1 { get; }
    public int Prop2 { get; }

    public SampleClass(string prop1, int prop2)
    {
        Prop1 = prop1;
        Prop2 = prop2;
    }
}

Further technical details

EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer

@ajcvickers
Copy link
Contributor

@codepb This is already implemented in the 2.1 code. You might consider giving it a try using our nightly builds, which are listed on the homepage: https://github.com/aspnet/EntityFrameworkCore

@codepb
Copy link
Author

codepb commented Jan 16, 2018

Excellent news, thanks! I didn't see an issue for this already, so apologies if I missed that.

@ajcvickers ajcvickers changed the title Construct by using Entity constructor Allow EF to call parameterized entity constructors Jan 17, 2018
@ajcvickers ajcvickers added this to the 2.1.0 milestone Jan 17, 2018
@ajcvickers ajcvickers self-assigned this Jan 17, 2018
@Tarig0
Copy link

Tarig0 commented Jan 19, 2018

Can we have Ef replace immutables, as described here?

Think currently with the use case you would have to manually detach the old entity and reattach the new entity.

@ajcvickers
Copy link
Contributor

Note for triage: work still to be done here:

  • Add fluent API for
    • Choosing the constructor to use/ignore
    • Configuring a factory method instead
    • Binding to a service

@ajcvickers
Copy link
Contributor

Closing this as fixed, with additional work tracked in #10789

@ajcvickers ajcvickers modified the milestones: 2.1.0, 2.1.0-preview1 Jan 27, 2018
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 27, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants