选择语句抛出null异常

时间:2013-01-04 19:57:54

标签: c# servicestack ormlite-servicestack

在第二个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;
}

1 个答案:

答案 0 :(得分:1)

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

财产,

Commz.Site_id被宣布:

public int Site_id { get; set; }

应该被声明为:

public int? Site_id { get; set; }