entity-framework - 在 EfCore 2.1 中处理 IReadOnlyCollection 属性
问题描述
我有以下域实体:
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] 属性只是为了看看发生了什么,但这没有用。
我真的错了吗?任何指针将不胜感激。
解决方案
可以为导航属性配置支持字段使用,但不能通过Property
用于原始属性的方法,也不能通过 fluent API(目前不存在),而是直接通过与关系关联的可变模型元数据:
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);
推荐阅读
- java - 在所有 Spring Batch Step 中共享 HSQLDB
- c# - 如何在C#中实现取消从PC删除文件夹、文件的过程并恢复已删除的信息?
- c# - 仅解析特定的文本/int
- parameters - Vue - 使用路由器链接将道具传递给路由器
- hadoop - SAS Hive SQL (Hadoop) 版本的 Proc Transpose?
- r - 用 t 分数 (2.5 SD) 间隔绘制骨矿物质密度 (BMD) 曲线?
- git - 用于创建 git changelog 的 Powershell 脚本
- mongodb - 由于 WiredTiger.turtle 权限,Mongodb 无法启动
- logic - 一阶逻辑 - 量词的位置
- mono - 在没有预编译的情况下在覆盆子上安装单声道