如何在 ef core 6 中配置引用另一个实体的拥有类型
How to configure Owned type referencing another entity in ef core 6
我有以下名为 Slot 的实体。
public class Slot : Entity
{
public virtual SnackPile SnackPile { get; set; }
}
这里的 SnackPile 是一个 ValueObject,没有自己的 table。它由插槽拥有。 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 有一个 属性 零食,这是一个实体。如何配置这个?
正如你在那里看到的,我试图添加这个
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')
卡住了:(有什么想法吗?
默认情况下,EF Core 仅映射具有 public getter 和任何 setter 的属性(原始或类似导航)(可以是私有的、受保护等)。
由于您的所有属性(包括有问题的属性)都是 仅获取(没有 setter),因此您必须明确映射它们。
对于原始属性,您使用 Property
流畅 API。但是对于导航属性,您需要流利的关系 API,例如Has
/ With
对。在你的情况下:
slotToSnackpile.HasOne(e => e.Snack).WithMany().IsRequired();
我有以下名为 Slot 的实体。
public class Slot : Entity
{
public virtual SnackPile SnackPile { get; set; }
}
这里的 SnackPile 是一个 ValueObject,没有自己的 table。它由插槽拥有。 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 有一个 属性 零食,这是一个实体。如何配置这个? 正如你在那里看到的,我试图添加这个
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')
卡住了:(有什么想法吗?
默认情况下,EF Core 仅映射具有 public getter 和任何 setter 的属性(原始或类似导航)(可以是私有的、受保护等)。
由于您的所有属性(包括有问题的属性)都是 仅获取(没有 setter),因此您必须明确映射它们。
对于原始属性,您使用 Property
流畅 API。但是对于导航属性,您需要流利的关系 API,例如Has
/ With
对。在你的情况下:
slotToSnackpile.HasOne(e => e.Snack).WithMany().IsRequired();