Reputation: 492
I'm starting making experience with the EF Code-First and also WCF Service and run into a problem I could not solve with all the guides about this issue:
I got the following code structure
[DataContract]
public class Feed
{
public int Id { get; set; }
public int LanguageId { get; set; }
public int CategoryId { get; set; }
public int TypeId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public FeedCategory Category { get; set; }
[DataMember]
public FeedType Type { get; set; }
[DataMember]
public string FeedUrl { get; set; }
[DataMember]
public Language Language { get; set; }
}
[DataContract]
public class FeedCategory
{
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public DateTime Registered { get; set; }
[DataMember]
public IList<Feed> Feeds { get; set; }
}
[DataContract]
public class FeedType
{
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public DateTime Registered { get; set; }
public IList<Feed> Feeds { get; set; }
}
[DataContract]
public class Language
{
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string CountryName { get; set; }
[DataMember]
public string CountryCode { get; set; }
[DataMember]
public string ShortCountryCode { get; set; }
}
But when I want to get all the feeds, all dependencies won't be received, so Category, Type & Language is null and I have no idea how to solve it.
Does anyone else knows how to do it?
Upvotes: 0
Views: 42
Reputation: 1714
I'm going to try and answer your question correctly based on my experience (from awhile ago as my company doesn't use EF anymore).
First you may need to apply a key to your entities. I did this in the OnModelCreating method.
modelBuilder.Entity<FeedType>().HasKey(k => k.Id );
Second I believe you have to set the mappings between these entities also able to be done in your OnModelCreating method.
modelBuilder.Entity<FeedType>().HasRequired<Feed>(h => h.Feed).WithOptional(x => x.FeedType);
Finally your need to enable eager loading or use the .Include on your query so that the child object is retrieved when the parent is.
All corrections welcome as it has been awhile.
Upvotes: 1