我遇到了一个时髦的问题,.SingleOrDefaultAsync
在我不期待的时候会发出错误。我的表格为Thing
,主键为varchar
ThingId
。
我的代码类似如下:
IDbSet<Thing> Things; // initialized by dependency injection
...
public async Task<Thing> FindThingById(string thingId)
{
return await Things.SingleOrDefaultAsync(o => o.ThingId == thingId).ConfigureAwait(false);
}
关于如何返回多个结果的任何想法,从而抛出一个异常说'Sequence contains more than one element'
??
当我运行SQL查询时:
SELECT * FROM Things where ThingId = '1234'
我得到了一个预期的结果。
编辑(调用代码):
public sealed partial class ThingDetailsManager : IThingDetailsManager
{
private readonly IThingRepository _thingRepository;
private readonly IThingDetailsSerializer _thingDetailsSerializer;
public async Task<ThingDetails> FetchThingDetails(string thingId)
{
var thing = await _orderRepository.FindThingById(thingId).ConfigureAwait(false);
if (thing == null)
return null;
var thingDetails = _thingDetailsSerializer.Deserialize(thing.ThingDetails);
thingDetails.SetThingId(thingId);
return thingDetails;
}
}