[英]EF Core: Any reason to not throw on QueryClientEvaluationWarning
EF Core has a feature where it can evaluate parts of an query in memory when this part can not be executed on the DB. EF Core 具有 一项功能,可以评估 memory 中无法在数据库上执行的部分查询。 It logs a warning when this happens.发生这种情况时,它会记录警告。
Optionally EF Core can be configured to throw an exception when that happens可以选择将 EF Core 配置为在发生这种情况时抛出异常
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True;")
.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); // <--
Since evaluating something in memory that the programmer intended to be executed in the DB can be a huge performance issue and is somewhat unclean imho I'm now thinking about using this exception configuration just everywhere and all the time.由于在 memory 中评估程序员打算在数据库中执行的内容可能是一个巨大的性能问题,而且恕我直言,我现在正在考虑随时随地使用此异常配置。
I'm just wondering if there are any good reasons to not throw an exception?我只是想知道是否有充分的理由不抛出异常? I personally would prefer an exception and then change the code to be executed intentionally in memory (eg with a .ToList()
before the problematic statement).我个人更喜欢异常,然后更改要在 memory 中有意执行的代码(例如,在有问题的语句之前使用.ToList()
)。 It's strange to me that EF would just work around a design flaw like that.令我感到奇怪的是,EF 会解决这样的设计缺陷。
However I'm unsure if I'm forgetting any good reason why this exists in the first place or any situation where this might be necessary.但是,我不确定我是否忘记了为什么首先存在这种情况的任何充分理由或可能需要这样做的任何情况。
A good reason to not throw an exception is when you use a non-relational database and where you would have to do data processing and searching in your code.不抛出异常的一个很好的理由是当您使用非关系数据库并且您必须在代码中进行数据处理和搜索时。 Another case can be if you use a linq query to combine data from multiple sources - in-memory data structure, XML, database, and you expect that you will only read from these sources and do all the processing in your code.另一种情况是,如果您使用 linq 查询来组合来自多个来源的数据 - 内存数据结构,XML,数据库,并且您希望您只会从这些来源读取并在代码中进行所有处理。
When you target relational databases only, however, you should always throw.但是,当您仅针对关系数据库时,您应该始终抛出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.