I am trying to follow DDD in a new project im working on and have run into an issue when it comes to configuring one of my ValueObjects.
I have the following AggregateRoot
public sealed class Event : TenantOwnedEntity<EventId>
{
... removed for clarity ...
private Event(EventId id,
TenantId tenantId,
EventName name,
EventDescription description,
DateOnly startDate,
DateOnly? endDate,
EventCost cost,
EventAttendiesType attendiesType)
: base(id, tenantId)
{
Name = name;
Description = description;
StartDate = startDate;
EndDate = endDate;
Cost = cost;
AttendeesType = attendiesType;
}
public EventName Name { get; private set; }
public EventDescription Description { get; private set; }
public DateOnly StartDate { get; private set; }
public DateOnly? EndDate { get; private set; }
public EventCost Cost { get; private set; }
public EventAttendiesType AttendeesType { get; private set; }
... removed for clarity ...
}
with the following configuration
internal class EventConfiguration : IEntityTypeConfiguration<Event>
{
public void Configure(EntityTypeBuilder<Event> builder)
{
builder.HasKey(e => e.Id);
builder
.Property(e => e.Id)
.HasConversion
(
id => id.Id,
value => new EventId(value)
);
builder.HasIndex(e => e.TenantId);
builder
.Property(e => e.Name)
.HasConversion
(
name => name.Value,
value => EventName.Create(value).Value
)
.HasColumnType($"varchar({EventName.MAX_LENGTH})")
.HasMaxLength(EventName.MAX_LENGTH);
builder
.Property(e => e.Description)
.HasConversion
(
description => description.Value,
value => EventDescription.Create(value).Value
)
.HasColumnType($"varchar({EventDescription.MAX_LENGTH})")
.HasMaxLength(EventDescription.MAX_LENGTH);
builder
.Property(e => e.Cost)
.HasConversion
(
cost => EventCostToDb(cost),
value => EventCostFromDb(value)
)
.HasColumnType("varchar(50)")
.HasMaxLength(50);
builder
.Property(e => e.AttendeesType)
.HasConversion
(
attendeeType => AttendeeTypeToDb(attendeeType),
value => AttendeeTypeFromDb(value)
)
.HasColumnType("varchar(50)")
.HasMaxLength(50);
}
The issue im having is that when I try to create a migration EF core 9 throws the following error.
Unable to create a 'DbContext' of type 'AppContext'. The exception 'No suitable constructor was found for entity type 'Event'. The following constructors had parameters that could not be bound to properties of the entity type: Cannot bind 'attendiesType' in 'Event(EventId id, TenantId tenantId, EventName name, EventDescription description, DateOnly startDate, DateOnly? endDate, EventCost cost, EventAttendiesType attendiesType)' Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
There is nothing special about the attendiesType it's a basic ValueObject just like EventCost which EF Core is not moaning about. Any suggestion as to what's going on here would be appreciated - I've been pulling my hair out about this one for a couple of hours now
The key info is this:
Note that only mapped properties can be bound to constructor parameters.
Your constructor argument is defined as this: attendiesType
.
And your property is defined as this: AttendeesType
.
Would you be able to spot the difference, as to why one is mismatched with the other? ^^