简体   繁体   English

使用LazyLoadedProxies时,efcore 2.2关闭特定查询的代理创建

[英]efcore 2.2 turn off proxy creation for a specific query when Using LazyLoadedProxies

I have an application where in most places I want to be able to use the LazyLoadedProxies so am calling UseLazyLoadingProxies in OnConfiguring. 我有一个应用程序,在大多数地方我都希望能够使用LazyLoadedProxies,因此在OnConfiguring中调用UseLazyLoadingProxies。

There are specific queries that I don't want proxies as I am going to serialize the object and others where it may be passed to another part of the application and I don't want any N+1 errors so want to ensure that I am not getting a Proxy. 在我要序列化对象以及其他可能将其传递到应用程序另一部分的对象时,我并不需要代理,因此我不希望出现任何N + 1错误,因此希望确保没有获得代理。

How can you disable Proxies for a specific query. 如何为特定查询禁用代理。

I don't want any N+1 errors so want to ensure that I am not getting a Proxy. 我不需要任何N + 1错误,因此想确保我没有代理。

No! 没有! you cannot disable proxy creation in EF Core query. 您不能在EF Core查询中禁用代理创建。 But for JSON serialization you can turn it off as follows: 但是对于JSON序列化,您可以按以下方式将其关闭:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}

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

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