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

前端 未结 5 952
感情败类
感情败类 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条回答
  •  Happy的楠姐
    2020-11-27 12:17

    Didnt work for me. But I solved it by doing like this.

    var item = db.Items
                 .Include(i => i.Category)
                 .Include(i => i.Brand)
                 .Where(x => x.ItemId == id)
                 .First();
    

    Dont know if thats a ok solution. But the other one Dennis gave gave me a bool error in .SingleOrDefault(x => x.ItemId = id);

提交回复
热议问题