I can’t figure out how to configure ef core to handle an ValueObject that needs to be persisted and used in multiple entities. I am trying to adhere to DDD. I have a ValueObject called GradingPeriod. Its a ValueObject because its determined by the Date Range and the Organization. However I can’t figure out how to persist it to the DB and also use it in multiple other entities that need to use it. Currently I have it ownedby the organization as they will be the controller of when grading periods begin and end. I need to be able to use it in things like a IEP for start and end grading periods and also in Goals or Objectives that have a grading period assigned to them.
Here is a snipped from organization:
builder.OwnsMany<GradingPeriod>(x => x.GradingPeriods, gradingPeriod =>
{
gradingPeriod.ToTable("organization_grading_period");
gradingPeriod.Property<int>("Id").HasColumnName("id");
gradingPeriod.HasKey("Id");
gradingPeriod.Property(x => x.OrganizationId).IsRequired().HasColumnName("organization_id");
gradingPeriod.Property(x => x.Name).IsRequired().HasMaxLength(100).HasColumnName("name");
gradingPeriod.OwnsOne(t => t.LocalDateRange, childBuilder =>
{
childBuilder.Property(x => x.Start).HasColumnName("date_start");
childBuilder.Property(x => x.End).HasColumnName("date_end");
});
});
Here is a snip from IEP Objective as you can see from the commented code I have been trying different things but getting different errors:
goal.OwnsMany(x => x.Objectives, objective =>
{
objective.ToTable("iep_objectives");
objective.Property(x => x.Id).ValueGeneratedNever();
goal.HasKey(x => x.Id);
objective.Property(x => x.GoalId).IsRequired().HasColumnName("goal_id");
objective.Property(x => x.Description).IsRequired().HasMaxLength(2000).HasColumnName("description");
// objective.Property(x => x.GradingPeriodStart).HasColumnName("grading_period_start");
objective.OwnsOne(x => x.GradingPeriodStart);
objective.OwnsOne(x => x.GradingPeriodDue);
// objective.HasOne(x => x.GradingPeriodDue).WithOne().HasPrincipalKey<GradingPeriod>(x => new {
// x.OrganizationId, x.Name
// });
// objective.Property(x => x.GradingPeriodDue).HasColumnName("grading_period_due");