0

When Entity Framework retrieves data from database and creates a new model class for that, how to pass values to the constructor instead of assigning them to the properties?

For example :

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Customer(string firstName, string lastName)
    {
    }
}

So when I call to _context.Customers.ToList(); I want to pass values to the constructor, instead of assigning them to the properties.

1
  • 3
    Please always tag the applicable EF version. The differences between versions can be significant, esp. core vs. "classic". EF-core now supports entity types with constructors. By the way, I don't see what calling ToList() has to do with constructors. Commented May 9, 2018 at 11:23

1 Answer 1

1

It's not possible, Entity Framework constructs entities using parameterless constructors and sets the properties value using setters. Although, neither parameterless constructor nor setters have to be public.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.