Skip to content

Allow EF to call parameterized entity constructors #10703

@codepb

Description

@codepb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions