【发布时间】:2022-01-25 12:47:05
【问题描述】:
我有一个名为 Slot 的实体。
public class Slot : Entity
{
public virtual SnackPile SnackPile { get; set; }
}
这里的SnackPile 是一个ValueObject,没有自己的表。它归插槽所有。 SnackPile 看起来像这样。
public sealed class SnackPile : ValueObject<SnackPile>
{
public static readonly SnackPile Empty = new SnackPile(Snack.None, 0, 0m);
public Snack Snack { get; } // Please note this Snack, we will come to this.
public int Quantity { get; }
public decimal Price { get; }
private SnackPile() { }
}
所以要配置它,我有以下内容。
modelBuilder.Entity<Slot>().OwnsOne(slot => slot.SnackPile, slotToSnackpile =>
{
slotToSnackpile.Property(ss => ss.Price).IsRequired();
slotToSnackpile.Property(ss => ss.Quantity).IsRequired();
//slotToSnackpile.Navigation(p => p.Snack).IsRequired(); // This is not working.
}).Navigation(slot => slot.SnackPile).IsRequired();
到目前为止一切顺利。
现在 SnackPile 具有 Snack 属性,这是一个实体。如何配置这个? 如您所见,我尝试添加此
slotToSnackpile.Navigation(p => p.Snack).IsRequired(); // This is not working.
它给出了以下错误。
Navigation 'SnackPile.Snack' was not found. Please add the navigation to the entity type before configuring it.
也尝试了以下两个,但没有成功。
//slotToSnackpile.Property(ss => ss.Snack).IsRequired();
这会产生以下错误。 The property 'SnackPile.Snack' is of type 'Snack' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
还有这个
//slotToSnackpile.Property(ss => ss.Snack.Id).IsRequired();
我得到了错误。 The expression 'ss => ss.Snack.Id' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. (Parameter 'memberAccessExpression')
卡住了:(有什么想法吗?
【问题讨论】:
-
请edit您的问题包含您的源代码作为工作minimal reproducible example,其他人可以编译和测试。
标签: c# ef-core-6.0