Entity Framework 核心实体 > 值对象 > 实体关系
Entity Framework Core Entity > Value Object > Entity Relationship
我有以下类
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 核心建立我的关系,但我的 SnackPiles 和 Snacks 在尝试时都是空的将它们加载到我的 UI 中。但是,如果我只设置我的 SnackMachines,我所有的 SnackPiles 都可以正常加载,但 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);
}
}
进一步的参考是,我正在学习 PluralSite 上使用 NHibernate 的 DDDInPractice 课程(这是一门很棒的课程,强烈推荐)。使用 EF 是一种了解细微差别的学习练习。课程所有者推荐我访问他关于该主题的博客 post,但自那时以来 EF 发生了变化。我对很多这些概念都有很好的理解,但我被困在这里。
列表中的第 6 位:
https://enterprisecraftsmanship.com/posts/ef-core-vs-nhibernate-ddd-perspective/
问题是我没有使用延迟加载。
我有以下类
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 核心建立我的关系,但我的 SnackPiles 和 Snacks 在尝试时都是空的将它们加载到我的 UI 中。但是,如果我只设置我的 SnackMachines,我所有的 SnackPiles 都可以正常加载,但 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);
}
}
进一步的参考是,我正在学习 PluralSite 上使用 NHibernate 的 DDDInPractice 课程(这是一门很棒的课程,强烈推荐)。使用 EF 是一种了解细微差别的学习练习。课程所有者推荐我访问他关于该主题的博客 post,但自那时以来 EF 发生了变化。我对很多这些概念都有很好的理解,但我被困在这里。
列表中的第 6 位: https://enterprisecraftsmanship.com/posts/ef-core-vs-nhibernate-ddd-perspective/
问题是我没有使用延迟加载。