如何在实体框架中正确实现多对一结构?

时间:2016-02-21 21:11:28

标签: c# entity-framework

我知道这是一个古老的问题,但我不能让它发挥作用,并且会喜欢人类的解释!

我的数据库中有四个表,并且只想进行一次api调用以获取数据 - 表格代表不同的实体,书籍,文章,案例和法规,我想要一个名为参考书目的模型,它可以解决所有这些问题。在一起。

我尝试过以下操作,但是在向表中添加数据并调用这个控制器之后,我得到了一个200 ok的[0]数据。

我的模特:

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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你应该像其他建议一样使用.Include()。如果您的表格具有适当的关系,则可以使用GetBibliographies api。

https://msdn.microsoft.com/en-us/data/jj574232.aspx

答案 1 :(得分:-2)

将方法更改为以下内容:

    let directoryURL: NSURL?
    do
    {
        directoryURL = try manager.URLForDirectory(.DocumentationDirectory,
                            inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
    }
    catch _
    {
        print("Error: call to manager.URLForDirectory(...) threw an exception")
    }