简体   繁体   English

如何从该导航属性获取值?

[英]How can I get a value from this navigation property?

New to Entity Framework and this is confusing me. 实体框架的新知识,这让我感到困惑。 I have 3 classes "Candidate", "Website" and "WebsiteType". 我有3个类别“候选”,“网站”和“ WebsiteType”。 Candidate has a one to many relationship with Website. 候选人与网站有一对多的关系。 WebsiteType is purely a lookup table to store the type of site being accessed. WebsiteType纯粹是用于存储要访问的网站类型的查找表。 I can return the collection of websites for a candidate but cannot figure out how to get the Name property stored in WebsiteType. 我可以为候选人返回网站集合,但无法弄清楚如何获取存储在WebsiteType中的Name属性。

Here are the 3 classes: 这是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; }
    }

Below is the code I am using to access the data and project to my DTO. 下面是我用来访问数据并将项目投影到我的DTO的代码。 Again my goal is to return the "Name" property from WebsiteType along with my Website collection. 同样,我的目标是从WebsiteType返回“名称”属性以及我的网站集合。 Could anyone point me in the right direction? 有人能指出我正确的方向吗?

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

And finally, this is my DTO class: 最后,这是我的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; }
    }

I would like to know how I could return the "SiteName" property from WebsiteType with my collection of Websites. 我想知道如何从我的网站集中从WebsiteType返回“ SiteName”属性。 Any help would be appreciated. 任何帮助,将不胜感激。

If you're using proxies (default these days) be sure to use the include function - .Include("Websites.WebsiteType"). 如果您使用代理服务器(目前是默认值),请确保使用包含功能-.Include(“ Websites.WebsiteType”)。 If you're storing the entity in your own DTO as a property and passing it around you might want to just disable proxies. 如果要将实体作为属性存储在自己的DTO中并传递给您,则可能只想禁用代理即可。

See here for more info - https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx 请参阅此处以获取更多信息-https: //msdn.microsoft.com/zh-cn/library/jj574232(v=vs.113).aspx

So something more like this: 所以更像这样:

 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();

Looking closer you might have an error on the Websites bit (essentially have Candidate.Candidate.Websites and I'm not seeing a CandidateID property in your EF class), probably should just be curCandidate.Websites instead of curCandidate.Candidate.Websites 仔细观察,您可能在“网站”位上有一个错误(本质上是Candidate.Candidate.Websites,并且我在您的EF类中没有看到CandidateID属性),可能应该只是curCandidate.Websites而不是curCandidate.Candidate.Websites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从匿名类型获取属性的值? - How can I get a value of a property from an anonymous type? 如何在后面的代码中设置导航属性的值? - How can I set the value of a Navigation Property in the code behind? 如何获得自定义属性的值? - How can i get the value of the custom property? 如何从对象中获取属性和值的名称并将此名称和值传递给 c# 中的列表 - How can i get the name of property and value from object and pass this name and value to a list in c# 我如何从属性中获取实例 - how can I get instance from the property 我怎样才能 map 这个导航属性? - How can I map this navigation property? 我可以从另一个获得一个附加财产的价值吗? - Can I get the value of one attached property from another? (首先是EF6模型)如何在不访问导航属性的情况下获取外键的值,从而无需加载整个实体? - (EF6 Model first) How can I get the value of a foreign key without accessing the navigation property so that I don't need to load the full entity? 如何在C#中使用反射从类型和设置属性值中按名称获取属性 - How can I Get the property by name from type and Set Propert value using reflection in C# 如何从元数据类中获取具有属性集的类的属性值? - How can I get the value of a property for a class with an attribute set from a metadata class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM