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

Ordering with Multiple constructors in the Entity type #29940

Closed
mehdihadeli opened this issue Dec 28, 2022 · 2 comments
Closed

Ordering with Multiple constructors in the Entity type #29940

mehdihadeli opened this issue Dec 28, 2022 · 2 comments

Comments

@mehdihadeli
Copy link

mehdihadeli commented Dec 28, 2022

Hi,
I have an aggregate, and I want to know what is the ordering algorithm in calling constructor of the entity?
Should I have a parameter constructor always in my aggregate? In many sample codes, for example here and here, I see this parameterless constructor. Maybe the reason of defining a parameterless constructor is that, in their parameters constructor they have some class as parameter and ef can't map them correctly, so for bypassing this parameter constructor they defined a parameterless one.

Does support injection class (navigation property classes) in entity type of .net 7, or it is in .net 8 plans?

This is my AggregateRoot class:

public class Customer : Aggregate<CustomerId>
{
    // EF
    private Customer()
    {
    }

    public Customer(
          CustomerId id,
          Email email,
          PhoneNumber phoneNumber,
          CustomerName name,
          Guid identityId)
      {
        //...
      }

    public Guid IdentityId { get; private set; }
    public Email Email { get; private set; } = default!;
    public CustomerName Name { get; private set; } = default!;
    public Address? Address { get; private set; }
    public Nationality? Nationality { get; private set; }
    public BirthDate? BirthDate { get; private set; }
    public PhoneNumber PhoneNumber { get; private set; } = default!;

    public static Customer Create(
        CustomerId id,
        Email email,
        PhoneNumber phoneNumber,
        CustomerName name,
        Guid identityId)
    {
        var customer = new Customer
        {
            Id = Guard.Against.Null(id),
            Email = Guard.Against.Null(email),
            PhoneNumber = Guard.Against.Null(phoneNumber),
            Name = Guard.Against.Null(name),
            IdentityId = Guard.Against.NullOrEmpty(identityId),
        };

        customer.AddDomainEvents(new CustomerCreated(customer));

        return customer;
    }
}

Include provider and version information

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.PostgreSql)
Target framework: (e.g. .NET 7.0)

@ajcvickers
Copy link
Member

@mehdihadeli EF doesn't need a parameterless constructor, but it does need a constructor with parameters that set only mapped properties of the entity, and not also other referenced entity instances. It's difficult to tell how your properties are mapped in the example code above, but if they are all owned typed--which means the parameters are all different entity instances--then EF can't currently can't set those in the constructor--this is tracked by #12078.

@mehdihadeli
Copy link
Author

@ajcvickers Thanks for your response.

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