Я создаю простой веб-API (MVC), и в настоящее время я использую подход Entity Framework Code First. У меня есть следующие объекты:
Авторы, курсы, институты
Я установил отношения таким образом, чтобы у автора могло быть 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"
}]
В этом я вижу что мы делаем какую-то нетерпеливую загрузку, а это не то, что я хочу. Как мне получить только данные об авторе?
Вот мои сгенерированные модели: Автор
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; }
}
}
Курсы:
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; }
}
}