简体   繁体   English

请求特定数据返回所有关联实体,而不使用 Include()

[英]Requesting specific data returns all the associated entities without using the Include()

I am creating a simple web API (MVC) and I am currently using Entity Framework Code First approach.我正在创建一个简单的 Web API (MVC),我目前正在使用实体框架代码优先方法。 I have the following entities:我有以下实体:

Authors, Courses, Institutions作者、课程、机构

I have set up a relationship in such a way that an author can have N number of courses and N number of institutions.我已经建立了这样一种关系,即作者可以拥有 N 个课程和 N 个机构。 I am writing the following method on the API:我正在 API 上编写以下方法:

public class PlutoController : ApiController
{
    private readonly PlutoDbContext _context;

    public PlutoController()
    {
        _context = new PlutoDbContext();
    }

    [HttpGet]
    public IHttpActionResult GetAuthors()
    {
        var authors = _context.Authors
            .Where(a => a.AuthorID == 1).ToList();

        return Ok(authors);
    }
}

However, a call to the api returns authors and its associated courses and institutions.但是,调用 api 会返回作者及其相关课程和机构。

[
{
    "Courses": [
        {
            "CourseSections": [],
            "Tags": [],
            "CourseID": 1,
            "AuthorID": 1,
            "Title": "C# Advanced",
            "Description": "C# Advanced Description",
            "Price": 69,
            "LevelString": "Advanced",
            "Level": 3
        },
        {
            "CourseSections": [],
            "Tags": [],
            "CourseID": 2,
            "AuthorID": 1,
            "Title": "C# Intermediate",
            "Description": "C# Intermediate Description",
            "Price": 49,
            "LevelString": "Intermediate",
            "Level": 2
        },
        {
            "CourseSections": [],
            "Tags": [],
            "CourseID": 3,
            "AuthorID": 1,
            "Title": "Clean Code",
            "Description": "Clean Code Description",
            "Price": 99,
            "LevelString": "Intermediate",
            "Level": 2
        }
    ],
    "Institution": [
        {
            "InstitutionId": 1,
            "AuthorId": 1,
            "InstitutionName": "University of Windsor"
        },
        {
            "InstitutionId": 2,
            "AuthorId": 1,
            "InstitutionName": "Boston University"
        },
        {
            "InstitutionId": 4,
            "AuthorId": 1,
            "InstitutionName": "Arizona State University"
        }
    ],
    "AuthorID": 1,
    "Name": "Bill Gates"
}]

In this, i can see that we are doing some eager loading which is not what i want.在这里,我可以看到我们正在做一些不是我想要的急切加载。 How do i just get only the author details?我如何只获得作者的详细信息?

Here are my generated models: Author这是我生成的模型:作者

namespace PlutoAPI.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class Authors
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Authors()
        {
            Institution = new HashSet<Institution>();
            Courses = new HashSet<Courses>();
        }

        [Key]
        public int AuthorID { get; set; }

        [Required]
        [StringLength(255)]
        public string Name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Institution> Institution { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Courses> Courses { get; set; }
    }
}

Courses:培训班:

namespace PlutoAPI.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class Courses
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Courses()
        {
            CourseSections = new HashSet<CourseSections>();
            Tags = new HashSet<Tags>();
        }

        [Key]
        public int CourseID { get; set; }

        public int AuthorID { get; set; }

        [Required]
        [StringLength(255)]
        public string Title { get; set; }

        [Required]
        [StringLength(8000)]
        public string Description { get; set; }

        public short Price { get; set; }

        [Required]
        [StringLength(50)]
        public string LevelString { get; set; }

        public byte Level { get; set; }

        public virtual Authors Authors { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<CourseSections> CourseSections { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Tags> Tags { get; set; }
    }
}

Institution机构

namespace PlutoAPI.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Institution")]
    public partial class Institution
    {
        public int InstitutionId { get; set; }

        public int? AuthorId { get; set; }

        [Required]
        [StringLength(50)]
        public string InstitutionName { get; set; }

        public virtual Authors Authors { get; set; }
    }
}

What problem you are having is LazyLoading (not eager loading).您遇到的问题是 LazyLoading(不是急切加载)。 To disable it.要禁用它。

public partial class YourDBContext : DbContext
{
    public YourDBContext(): base("name=YourDBContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
}

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

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