如何禁用ef.NETCore2API检索导航属性

i2byvkas  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(163)

我已经使用ef pomelo.entityframeworkcore.mysql创建了dbcontext和模型,我想知道如何在尝试获取与其他表有foriegnkey关系的对象时禁止上下文检索不必要的信息。现在api通过foreignkey导航返回对象及其相关对象。
例如,它返回:

我想这样归还:

比如说,我想让它停在服务台。它是服务内部的icollection变量。因为服务和额外都有一个多对多关系的第三个表。
可能的原因:在函数中,在检索服务之后,我检索了三个对象

Service = await _context.Service.SingleOrDefaultAsync(m => m.Idservice == id);

我检索另一个名为serviceextra的对象,现在服务有serviceextra导航字段,所以我调用这个函数

var serviceExtras = await _context.ServiceExtra.Where(m => m.Fkservice == id).ToListAsync();

它还填充服务对象的serviceextra内的数据。如果我不调用这个函数,它就不会。
启动类:

public class Startup
{
    public Startup(IConfiguration configuration,IHostingEnvironment hostingEnvironment)
    {

        Configuration = configuration;
        HostingEnvironment = hostingEnvironment;

    }
    public  IConfiguration Configuration { get; }
    public IHostingEnvironment HostingEnvironment { get; }
    public void ConfigureServices(IServiceCollection services)
    {

        var connection = Configuration["ConnectionStrings:connection"];
        services.AddDbContext<Db_context>(options => options.UseMySql(connection));
        services.AddMvc();
        services.AddMvc().AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseMvc();

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题