参见本例
现在我不做前2,我只是做一个简单的实体查找,所以我有点不知道为什么。
答案 0 :(得分:3)
您很可能使用SingleOrDefault
或Find
。这些方法通过选择前两行来工作。然后确保只返回一行。如果返回了多个元素,那么它将抛出异常。
如果有多场比赛,怎么知道?
// If there were two Products with a ProductName of Widget then the code would throw an exception.
context.Products.SingleOrDefault(i => i.ProductName == "Widget");
// If there were two Products with a ProductId of 1234 then the code would throw an exception.
context.Products.Find(1234);