如何从此导航属性中获取值?

时间:2018-03-15 16:46:04

标签: c# asp.net entity-framework

实体框架新手,这让我感到困惑。我有3个班级"候选人","网站"和" WebsiteType"。候选人与网站有一对多的关系。 WebsiteType纯粹是一个查找表,用于存储正在访问的站点类型。我可以为候选人返回网站集合,但无法弄清楚如何将Name属性存储在WebsiteType中。

以下是3个班级:

Candidate.cs
public partial class Candidate
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Candidate()
        {
            this.Websites = new HashSet<Website>();
        }

        public int Id { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleInitial { get; set; }
        public string NickName { get; set; }
        public string PhotoUrl { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public Nullable<int> NumberOfSignatures { get; set; }
        public Nullable<bool> IncludesSupplementalFiling { get; set; }
        public string Bio { get; set; }
        public Nullable<int> NumberOfVotes { get; set; }
        public Nullable<int> Percentage { get; set; }
        public Nullable<int> CategoryId { get; set; }
        public Nullable<int> OfficeId { get; set; }
        public Nullable<int> PoliticalPartyId { get; set; }

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

Website.cs
public partial class Website
    {
        public int WebsiteId { get; set; }
        public string Url { get; set; }
        public Nullable<int> WebsiteTypeId { get; set; }
        public Nullable<int> CandidateId { get; set; }

        public virtual Candidate Candidate { get; set; }
        public virtual WebsiteType WebsiteType { get; set; }
    }

WebsiteType.cs
 public partial class WebsiteType
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public WebsiteType()
        {
            this.Websites = new HashSet<Website>();
        }

        public int WebsiteTypeId { get; set; }
        public string SiteName { get; set; }

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

以下是我用来访问数据和项目到我的DTO的代码。我的目标是再次返回&#34;名称&#34;来自WebsiteType的财产以及我的网站集。有人能指出我正确的方向吗?

public CandidateDetailsDTO GetCandidateDetails(int id)
        {
            db.Configuration.LazyLoadingEnabled = false;
            var candidate = (from w in db.Websites
                             join c in db.Candidates on w.CandidateId equals c.Id
                             join wt in db.WebsiteTypes on w.WebsiteTypeId equals wt.WebsiteTypeId
                             select new CandidateDetailsDTO
                            {
                                Id = c.Id,
                                LastName = c.LastName,
                                FirstName = c.FirstName,
                                Bio = c.Bio,
                                Address = c.Address,
                                CategoryName = c.Category.CategoryName,
                                PoliticalParty = c.PoliticalParty.PartyName,
                                Office = c.Office.OfficeName,
                                Websites = w.Candidate.Websites
                            }).FirstOrDefault(c => c.Id == id);

最后,这是我的DTO课程:

public class CandidateDetailsDTO
    {
        public int Id { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleInitial { get; set; }
        public string NickName { get; set; }
        public string PhotoUrl { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Bio { get; set; }
        public string CategoryName { get; set; }
        public string PoliticalParty { get; set; }
        public string Office { get; set; }

        public ICollection<Website> Websites { get; set; }
    }

我想知道如何返回&#34; SiteName&#34;来自WebsiteType的财产以及我的网站集。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果你正在使用代理(这些天默认),请务必使用include函数 - .Include(&#34; Websites.WebsiteType&#34;)。如果您将实体作为属性存储在自己的DTO中并传递它,则可能只想禁用代理。

有关详细信息,请参阅此处 - https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

更像是这样的事情:

 var candidate = (from curCandidate in db.Candidates.Include("Category").Include("PoliticalParty").Include("Office").Include("Websites").Include("Websites.WebsiteType")
                                where curCandidate.Id == id
                                select new CandidateDetailsDTO
                                {
                                    Id = curCandidate.Id,
                                    LastName = curCandidate.LastName,
                                    FirstName = curCandidate.FirstName,
                                    Bio = curCandidate.Bio,
                                    Address = curCandidate.Address,
                                    CategoryName = curCandidate.Category.CategoryName,
                                    PoliticalParty = curCandidate.PoliticalParty.PartyName,
                                    Office = curCandidate.Office.OfficeName,
                                    Websites = curCandidate.Candidate.Websites
                                }).FirstOrDefault();

仔细观察,您可能会在网站上出现错误(基本上有Candidate.Candidate.Websites,而且我没有在您的EF类中看到CandidateID属性),可能应该只是curCandidate.Websites而不是curCandidate.Candidate.Websites而不是async