选择抛出空异常的语句

本文关键字:语句 异常 选择 | 更新日期: 2023-09-27 17:50:52

在第二个Select语句中获得一个神秘的null exception

public static string GetSNFromDeviceMacAddress(string macAddress)
{
    string commzSerialNumber = null;
    var dbFactory = EndpointHost.AppHost.TryResolve<IDbConnectionFactory>();
    try
    {
        OrmDataLib.Device device;
        OrmDataLib.Commz commz;
        using (var db = dbFactory.OpenDbConnection())
        {
            // first select works fine
            device = db
                .Select<OrmDataLib.Device>(q => q.MacAddress == macAddress)
                .FirstOrDefault();
            if (null != device)
            {
                // mysterious null exception
                commz = db
                    .Select<OrmDataLib.Commz>(c => c.Id == device.Commz_Id)
                    .FirstOrDefault();
                if (null != commz)
                    commzSerialNumber = commz.SerialNumber;
            }
        }
    }
    catch { }
    return commzSerialNumber;
}

选择抛出空异常的语句

原来接收DTO应该允许一个空值,而不是。

属性,

Commz.Site_id被声明:

public int Site_id { get; set; }

和应该声明为:

public int? Site_id { get; set; }