【问题标题】:Entity Framework Core Entity > Value Object > Entity Relationship实体框架核心实体 > 值对象 > 实体关系
【发布时间】:2020-01-27 19:18:24
【问题描述】:

我有以下课程

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;
    }
}

我正在尝试使用 Entity Framework Core 建立我的关系,但我的 SnackPiles 和 Snacks 在尝试将它们加载到我的 UI 时都为空。但是,如果我只设置我的 SnackMachines,我所有的 SnackPiles 都可以正常加载,但会有 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");
            });
        });
    }
}

我有两个问题。这样做,我得到了一个名为 SnackPile_SnackId 的影子属性,我想将它命名为 SnackId 但如果不同时创建属性和 SnackPile_SnackId,我什么都做不了设置为 FK。

下一个问题是.. 这种关系在 Entity Framework Core 3 中是否可以实现?看来我有一个实体,它的值对象包含我想引用的另一个实体的 Id。

我想得到的结果可以用 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);
    }
}

进一步的参考是我正在学习使用 NHibernate 的 PluralSite 上的 DDDInPractice 课程(这是一门很棒的课程,强烈推荐)。使用 EF 是一项了解细微差别的学习练习。课程所有者向我推荐了他关于该主题的博客文章,但从那时起 EF 发生了变化。我对很多这些概念都有很好的理解,但我被困在这里。

列表中的第 6 位: https://enterprisecraftsmanship.com/posts/ef-core-vs-nhibernate-ddd-perspective/

【问题讨论】:

    标签: entity-framework-core entity domain-driven-design relationship value-objects


    【解决方案1】:

    问题是我没有使用延迟加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2019-12-08
      • 2021-12-04
      • 1970-01-01
      • 2020-01-26
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多