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

C# 请求特定数据将返回所有关联的实体,而不使用Include(),c#,entity-framework-6,C#,Entity Framework 6,我正在创建一个简单的WebAPI(MVC),目前我正在使用实体框架代码优先的方法。我拥有以下实体: 作者、课程、机构 我已经建立了这样一种关系,一个作者可以有N个课程和N个机构。我正在API上编写以下方法: public class PlutoController : ApiController { private readonly PlutoDbContext _context; public PlutoController() { _context

我正在创建一个简单的WebAPI(MVC),目前我正在使用实体框架代码优先的方法。我拥有以下实体:

作者、课程、机构

我已经建立了这样一种关系,一个作者可以有N个课程和N个机构。我正在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);
    }
}
但是,调用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"
}]
在这方面,我可以看到,我们正在做一些渴望加载,这不是我想要的。我如何只获取作者的详细信息

以下是我生成的模型: 作者


您遇到的问题是懒散的加载(而不是急切的加载)。禁用它

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

我猜在你的背景下是真的?这对我来说很有用!!谢谢:)但我不明白这是怎么回事。急切加载不意味着在一个查询中加载所有实体吗?急切加载意味着在加载主实体时加载相关实体。但是您需要使用Include()来实现这一点。在您的情况下,因为您不使用它,所以当您使用ToList()时,它不会加载相关的实体。但是,当您“返回”它时,序列化程序开始访问这些属性,EF开始加载这些属性。
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; }
    }
}
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; }
    }
}
public partial class YourDBContext : DbContext
{
    public YourDBContext(): base("name=YourDBContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
}