简体   繁体   English

带有属性筛选器的 EF Core 支持字段

[英]EF Core backing field with filter on property

Consider the following考虑以下

public class Order
{
    private readonly List<OrderLineItem> lineItems = new();  
    public IReadOnlyCollection<OrderLineItem> LineItems => lineItems;
}

and in the DbContext mapper在 DbContext 映射器中

builder.HasMany(u => u.LineItems).WithOne().HasForeignKey(u => u.ProviderOrderId);
builder.Metadata.FindNavigation(nameof(ProviderOrder.LineItems))!.SetPropertyAccessMode(PropertyAccessMode.Field);

I want to add a filter to the LineItems property like this我想像这样向LineItems属性添加一个过滤器

public IReadOnlyCollection<OrderLineItem> LineItems => new ReadOnlyCollection<OrderLineItem>(lineItems.Where(u => !u.IsDeleted).ToList())

Can this potentially have any impact on the data that is being saved (ie accidentally filtering out the "soft" deleted records and deleting them permanently)?这可能会对正在保存的数据产生任何影响(即意外过滤掉“软”删除的记录并永久删除它们)?

From my understanding EF Core will only read to and from the backing field exclusively, and the property is only for setting up the initial mapping, and also for public access to the data.根据我的理解,EF Core 只会专门读取和读取支持字段,并且该属性仅用于设置初始映射,也用于对数据的公共访问。

Googling for people doing similar things didn't bring much up, so this is a sense check essentially.谷歌搜索做类似事情的人并没有带来太多结果,所以这本质上是一种感觉检查。

Thanks for easing my mind.谢谢你安慰我。

Through my own testing, I'm now satisfied that it's perfectly fine to apply filters to the property.通过我自己的测试,我现在很满意将过滤器应用于该属性是完全正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM