C# Entity-Framework: How can I combine a .Find and .Include on a Model Object?

前端 未结 5 934
感情败类
感情败类 2020-11-27 11:36

I\'m doing the mvcmusicstore practice tutorial. I noticed something when creating the scaffold for the album manager (add delete edit).

I want to write code elegantl

5条回答
  •  清酒与你
    2020-11-27 12:33

    You can use Include() first, then retrieve a single object from the resulting query:

    Item item = db.Items
                  .Include(i => i.Category)
                  .Include(i => i.Brand)
                  .FirstOrDefault(x => x.ItemId == id);
    

提交回复
热议问题