I have the following code:
using (var db = new IntDB())
{
var subscription =
(from s in db.Subscription
where s.SubsciptionId == subscriptionId
select s).FirstOrDefault();
if (subscription != null)
{
db.DeleteObject(subscription);
db.SaveChanges();
EntityKeyMember articleId = (EntityKeyMember)subscription.ArticleReference
.EntityKey.EntityKeyValues.GetValue(0);
var article = (from a in db.Article
where a.ArticleId == (int)articleId.Value
select a).FirstOrDefault();
if (!String.IsNullOrEmpty(article.WebUrl) && article.WebUrl.Equals(@"/ExploderLists"))
{
var lstAppIntrfc = new ListAppInterface();
// the articleId is stored in the entity key here, the article object hasn't been instanicated
// so it's easier to just get it from the EntityKey.
lstAppIntrfc.RemoveEmailFromListByArticleID((int)articleId.Value, subscription.EmailAddress);
}
}
}
and here’s my question. After I load the Subscription object with the LINQ code, I find that the Article property of the subscription instance is NULL! I can find the entityKey for the article in the subscription instance, but I have to then run LINQ to load the article instance that I need for the final IF statement there.
Have I just gone totally off the reservation here and I am not understanding how to use the entity objects or is this the only way to do this?
Use the
Includemethod to eager load theArticlewith theSubscription.