1

I m working on MVC5 project and there's problem with related items when retrieved.

1st I've some models like: Menu, MenuContents one-to-many related, menu has many menuContents with different languages

public class Menu
{
    public int ID { get; set; }
    public virtual List<MenuContent> MenuContents {get; set;}
    ...
}

public class MenuContent
{
    public int ID { get; set; }
    public int MenuID { get; set; }
    public virtual Menu Menu {get; set;}
    ...
}

But when I using menu object's menuContents there's sometimes no items or sometimes not all items there.

To describe well: There's 2 contents for 1 menu. Sometimes there's only 1 item in menu's menuContents property but sometimes it's zero or maybe 2.

Wondering what's wrong here. Furthermore missing items are so few only sometimes appears. Why are only some appearing?

  • Plus: I m using Data Annotations. – Tugsuu Ganbat Mar 06 '15 at 05:44
  • Do you mean the objects are not correctly populated? Or that they are not showing in a view? And what Data Annotations (there are none shown in your code)? You need to explain whats happening and include the relevant code. –  Mar 06 '15 at 05:58
  • Added formatting, clarified question. –  Mar 06 '15 at 06:00
  • Not correctly populated. Above I added only property without annotations. `[Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } [InverseProperty("Menu")] public virtual List MenuContents { get; set; }` `[ForeignKey("MenuID")] public virtual Menu Menu { get; set; }` – Tugsuu Ganbat Mar 06 '15 at 06:09

1 Answers1

1

The problem was that I wasn't using one context per request. Which means I was using new DBContext in every controller.

When I used 1 DBContext per request my problem solved /thanks for this post/.

Hope this helps someone later.

Community
  • 1
  • 1