我知道这是一个古老的问题,但我只是无法使它发挥作用,我会喜欢一个人类的解释!
我在我的数据库中有四个表,我只想调用一个api来获取数据--这些表代表不同的实体、书籍、文章、案例和法规,我想要一个叫做书目的模型,它把所有这些都整合在一起。
我尝试了以下方法,但是在向表中添加数据并调用此表的控制器后,我得到了一个200 ok的数据。
My models:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PhDResourceManager.Models
{
public class Journal
{
public Journal() { }
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string PublicationYear { get; set; }
public string PublicationName { get; set; }
public string PageNumber { get; set; }
public virtual Bibliography Bibliography { get; set; }
}
public class Book
{
public Book() { }
[Key]
public int ID { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public string Edition { get; set; }
public string PublicationYear { get; set; }
public string PublisherName { get; set; }
public string PageNumber { get; set; }
public virtual Bibliography Bibliography { get; set; }
}
public class Case
{
public Case() { }
[Key]
public int ID { get; set; }
public string Parties { get; set; }
public string NeutralCitation { get; set; }
public string LawReportsCitation { get; set; }
public virtual Bibliography Bibliography { get; set; }
}
public class Statute
{
public Statute() { }
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Section { get; set; }
public string Year { get; set; }
public virtual Bibliography Bibliography { get; set; }
}
public class Bibliography
{
public Bibliography()
{
MyJournals = new List<Journal>();
MyCases = new List<Case>();
MyBooks = new List<Book>();
MyStatutes = new List<Statute>();
}
[Key]
public int ID { get; set; }
public virtual ICollection<Journal> MyJournals { get; set; }
public virtual ICollection<Case> MyCases { get; set; }
public virtual ICollection<Book> MyBooks { get; set; }
public virtual ICollection<Statute> MyStatutes { get; set; }
}
}我的控制器:
namespace PhDResourceManager.Controllers
{
public class BibliographyController : ApiController
{
private PhDResourceManagerContext db = new PhDResourceManagerContext();
// GET: api/Bibliography
public IQueryable<Bibliography> GetBibliographies()
{
return db.Bibliographies;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool BibliographyExists(int id)
{
return db.Bibliographies.Count(e => e.ID == id) > 0;
}
}
}发布于 2016-02-21 22:49:47
将方法更改为:
public Bibliography GetBibliographies()
{
Bibliography bibliography = new Bibliography();
bibliography.MyJournals = db.Journal.ToList();
bibliography.MyCases = db.Cases.ToList();
bibliography.MyBooks = db.Books.ToList();
bibliography.MyStatutes = db.Statutes.ToList();
return bibliography;
}发布于 2016-02-22 00:09:47
您应该像其他人建议的那样使用.Include()。如果您的表具有适当的关系,它们将与您的GetBibliographies api一起下降。
https://stackoverflow.com/questions/35542098
复制相似问题