0

Following is my entities.

public class Expense
{
   [Key]
   [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ExpenseId { get; set; }

   [Required]
   public int CategoryId{ get; set; }
   [ForeignKey("CategoryId")]
   public virtual Category Category { get; set; }

   public virtual List<EditedExpense> EditedExpenses { get; set; }
}
public class EditedExpense
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int EditedExpenseId { get; set; }

    [Required]
    public int CategoryId{ get; set; }
    [ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }

    public int ExpenseId { get; set; }

}
public class Category
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int CategoryId{ get; set; }

    public string Title

    public virtual List<Expense> Expenses { get; set; }
    public virtual List<EditedExpense> EditedExpenses { get; set; }
}

I have used this code to get all Expanses and EditedExpanses

  var expenses = db.Expenses.Include(exp => exp.EditedExpenses).Include(exp => exp.Category);
   return View(expenses.ToList());

Now I want to go through all Expanses and thier EditedExpanse using two foreach loop like following, but it casts an exception when it tries to get the Category of the EditedExpense:

foreach(exp in expansesList)
{
    foreach(editedExp in exp.EditedExpanses)
    {
       <text>@editedExp.Category.Title</text>
    }
}
user217648
  • 3,338
  • 9
  • 37
  • 61
  • you include Category in Expense, not in EditedExpenses. – tschmit007 Jul 18 '14 at 08:55
  • Also, if you are using Includes everywhere, it's may be worth disabling lazy loading entirely. – DavidG Jul 18 '14 at 09:05
  • [this post](http://stackoverflow.com/questions/18398356/how-to-solve-the-error-the-objectcontext-instance-has-been-disposed-and-can-no-l) is worth reading what lazy loading and proxy is that related to your error – Yuliam Chandra Jul 18 '14 at 09:11
  • I disabled the Lazy Loading using db.Configuration.LazyLoading = false, but it genrated this error after that. Object reference not set to an instance of an object – user217648 Jul 18 '14 at 09:14

1 Answers1

2
var expenses = db.Expenses
                 .Include(exp => exp.EditedExpenses)
                 .Include(exp => exp.Category);

This includes the Category property of Expense, not the one of EditExpense.

Use:

var expenses = db.Expenses
                 .Include(exp => exp.EditedExpenses.Select(z => z.Category));

EDIT: Just to clarify about the exception you had: you currently are using lazy loading, so you can't lazy load navigation properties once your EF context has been disposed. As a side note, I always recommend deactivating lazy loading. See https://stackoverflow.com/a/21379510/870604

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 1
    your code is compile error, Include belongs to IQueryable not List!! should be `db.Expenses.Include(exp => exp.EditedExpenses.Select(z => z.Category))` – Yuliam Chandra Jul 18 '14 at 09:07
  • Thanks, regarding LazyLoadeing I changed it to false then it generated Object reference not set to an instance of an object – user217648 Jul 18 '14 at 09:13
  • 1
    @user217648 If you get a NRE it means you didn't include all the navigation properties you're using. Personally I prefer having a NRE (which is always reproducible) when I forget an Include than other nasty errors related to lazy loading. – ken2k Jul 18 '14 at 09:30