in

EF Core 5 Owned type does not return saved data from database


I’m implementing a small project aplying DDD on it, but I’m having problems with Value Objects.

I have the following Term entity:

public class Term : Entity<TermId>, IAggregateRoot
{

    #region Properties

    public string Code { get; private set; }
    public string Description { get; private set; }
    public DateRange EnrollmentDates { get; private set; }
    public DateRange TermDates { get; private set; }

    #endregion

    #region Property Setters

    public void SetCode(string code) => Code = code;

    public void SetDescription(string description) => Description = description;

    public void SetEnrollmentDates(DateRange enrollmentDates) => EnrollmentDates = enrollmentDates;

    public void SetTermDates(DateRange termDates) => TermDates = termDates;

    #endregion

    #region Ctors

    public Term()
    {

    }

    public Term(TermId termId, string code, string description, DateRange enrollmentDates, DateRange termDates)
    {
        Id = termId;
        Code = code;
        Description = description;
        EnrollmentDates = enrollmentDates;
        TermDates = termDates;
    }

    #endregion

}

And this is my DateRange value object:

public class DateRange : ValueObject
{

    #region Properties

    public DateTime From { get; private set; }
    public DateTime To { get; private set; }

    #endregion

    #region Ctor

    public DateRange(DateTime from, DateTime to)
    {
        From = from;
        To = to;
    }

    #endregion

    #region ValueObject Implementation

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return From;
        yield return To;
    }

    #endregion

}

With the following EF Configuration:

public class TermConfiguration : IEntityTypeConfiguration<Term>
{

    /// <summary>
    /// Configures every property in Term entity
    /// </summary>
    /// <param name="builder">EntityTypeBuilder object</param>
    public void Configure(EntityTypeBuilder<Term> builder)
    {
        builder
            .HasKey(u => u.Id);

        builder
            .Property(u => u.Id)
            .HasColumnType("varchar(255)")
            .HasMaxLength(255)
            .HasConversion(x => x.Value, x => new TermId(x));

        builder
            .Ignore(x => x.DomainEvents);

        builder
            .Property(u => u.Code)
            .IsRequired()
            .HasColumnType("varchar(50)")
            .HasMaxLength(50);

        builder
            .HasIndex(x => x.Code)
            .IsUnique();

        builder
            .Property(u => u.Description)
            .IsRequired()
            .HasColumnType("varchar(255)")
            .HasMaxLength(255);

        builder.OwnsOne<DateRange>("EnrollmentDates", b =>
        {
            b.WithOwner();

            b
                .Property(p => p.From)
                .HasColumnName("EnrollmentStartingDate")
                .IsRequired();

            b
                .Property(p => p.To)
                .HasColumnName("EnrollmentEndingDate")
                .IsRequired();
        }).Navigation(p => p.EnrollmentDates).IsRequired();

        builder.OwnsOne<DateRange>("TermDates", b =>
        {

            b
                .Property(p => p.From)
                .HasColumnName("TermStartingDate")
                .IsRequired();

            b
                .Property(p => p.To)
                .HasColumnName("TermEndingDate")
                .IsRequired();
        }).Navigation(p => p.TermDates).IsRequired();

    }

}

With the previous configuration, I can save a term with EnrollmentDates and TermDates, but when I want to retrieve this data from DB, value objects are null. When I call _termRepository.GetAll(); EF makes the following SQL Query, so it is correct:

SELECT [t].[Id], [t].[Code], [t].[Description], [t].[EnrollmentStartingDate], [t].[EnrollmentEndingDate], [t].[TermStartingDate], [t].[TermEndingDate] FROM [Terms] AS [t]

I’ve been trying everything I’ve found on the internet with no luck. I’m using ASP.NET 5.0 and EF Core 5.0.9. Is there anything I’m missing or something?



Source: https://stackoverflow.com/questions/70541488/ef-core-5-owned-type-does-not-return-saved-data-from-database

Java – code will always throw a Null Pointer Exception when no Property is present?

Small self-contained pure-Go web server with Lua, Markdown, HTTP/2, QUIC, Redis and PostgreSQL support