我有以下域名实体:
public string Reference { get; private set; }
public int SupplierId { get; private set; }
public int BranchId { get; private set; }
public Guid CreatedBy { get; private set; }
public DateTime CreatedDate { get; private set; }
public Source Source { get; private set; }
public OrderStatus OrderStatus { get; private set; }
public decimal NetTotal { get; private set; }
public decimal GrossTotal { get; private set; }
private List<PurchaseOrderLineItem> _lineItems = new List<PurchaseOrderLineItem>();
public IReadOnlyCollection<PurchaseOrderLineItem> LineItems => _lineItems.AsReadOnly();
我对订单项有以下配置:
builder.Property(x => x.LineItems)
.HasField("_lineItems")
.UsePropertyAccessMode(PropertyAccessMode.Field);
但是,当我运行我的应用时,我收到以下错误:
The property 'PurchaseOrder.LineItems' is of type 'IReadOnlyCollection<PurchaseOrderLineItem>' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
我的理解是EF应该只根据我的配置使用支持字段?
我尝试添加[NotMapped]属性只是为了看看发生了什么,但是没有用。
我真的错了吗?任何指针都会受到赞赏。
答案 0 :(得分:2)
可以为导航属性配置支持字段用法,但不能通过基本属性的Property
方法,而不是通过流畅的API(此时不存在),而是直接通过mutable与关系相关的模型元数据:
modelBuilder.Entity<PurchaseOrder>()
.HasMany(e => e.LineItems)
.WithOne(e => e.PurchaseOrder) // or `WithOne() in case there is no inverse navigation property
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field); // <--
您还可以使用以下方法设置所有实体导航属性的模式(您仍可以为各个属性覆盖它):
modelBuilder.Entity<PurchaseOrder>()
.Metadata.SetNavigationAccessMode(PropertyAccessMode.Field);