visualbam
visualbam

Reputation: 167

Entity Framework Core Entity > Value Object > Entity Relationship

I have the following classes

public class Slot : Entity
{
    public SnackPile SnackPile { get; set; }
    public SnackMachine SnackMachine { get; set; }
    public int Position { get; }

    protected Slot()
    {
    }

    public Slot(SnackMachine snackMachine, int position)
    {
        SnackMachine = snackMachine;
        Position = position;
        SnackPile = SnackPile.Empty;
    }
}
public class Snack : AggregateRoot
{
    public string Name { get; set; }

    public Snack()
    {
    }

    private Snack(long id, string name)
    {
        Id = id;
        Name = name;
    }
}
public class SnackPile : ValueObject
{
    public Snack Snack { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }

    protected SnackPile()
    {
    }

    public SnackPile(Snack snack, int quantity, decimal price)
    {
        Snack = snack;
        Quantity = quantity;
        Price = price;
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Snack;
        yield return Quantity;
        yield return Price;
    }
}

I'm trying to build my relationships using Entity Framework Core but my SnackPiles and Snacks are all null when trying to load them in my UI. However if I only set up my SnackMachines, all my of SnackPiles load fine but have null Snacks.

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<SnackMachine>(entity =>
        {
            entity.OwnsOne<Money>(e => e.MoneyInside, MoneyInside =>
            {
                MoneyInside.Property(mi => mi.OneCentCount).HasColumnName("OneCentCount");
                MoneyInside.Property(mi => mi.TenCentCount).HasColumnName("TenCentCount");
                MoneyInside.Property(mi => mi.QuarterCentCount).HasColumnName("QuarterCentCount");
                MoneyInside.Property(mi => mi.OneDollarCount).HasColumnName("OneDollarCount");
                MoneyInside.Property(mi => mi.FiveDollarCount).HasColumnName("FiveDollarCount");
                MoneyInside.Property(mi => mi.TenDollarCount).HasColumnName("TenDollarCount");
            }).Ignore(o => o.MoneyInTransaction);
        });

        builder.Entity<Slot>(entity =>
        {
            entity.Property(e => e.Position);
            entity.OwnsOne<SnackPile>(e => e.SnackPile, SnackPile =>
            {
                SnackPile.Property(sp => sp.Quantity).HasColumnName("Quantity");
                SnackPile.Property(sp => sp.Price).HasColumnName("Price").HasColumnType("Decimal");
            });
        });
    }
}

I have two questions. Doing this, I get a shadow property called SnackPile_SnackId which I would like named SnackId but nothing I do accomplishes this without creating both properties and the SnackPile_SnackId is set up as the FK.

The next question, is.. is this relationship attainable in Entity Framework Core 3? It appears I have an Entity that has a value object containing the Id of another Entity which I would like to reference.

The result I would like to get can be done with NHibernate

public class SlotMap : ClassMap<Slot>
{
    public SlotMap()
    {
        Id(x => x.Id);
        Map(x => x.Position);

        Component(x => x.SnackPile, y =>
        {
            y.Map(x => x.Quantity);
            y.Map(x => x.Price);
            y.References(x => x.Snack).Not.LazyLoad();
        });

        References(x => x.SnackMachine);
    }
}

Further reference is that I'm following the DDDInPractice course on PluralSite which uses NHibernate (It's an amazing course and highly recommend). Using EF is a learning exercise to see the nuances. The course owner referred me to his blog post on the subject but there have been changes to EF since then. I have an ok understanding for a lot of these concepts but I'm stuck here.

Number 6 in the list: https://enterprisecraftsmanship.com/posts/ef-core-vs-nhibernate-ddd-perspective/

Upvotes: 1

Views: 134

Answers (1)

visualbam
visualbam

Reputation: 167

The problem is that I wasn't using lazy loading.

Upvotes: 1

Related Questions