【发布时间】:2017-09-22 10:20:17
【问题描述】:
我正在尝试使用以下代码选择一条记录:
Location item = connection
.Table<Location>()
.Where(l => l.Label.Equals(label))
.FirstOrDefault();
这会导致:
System.NullReferenceException:对象引用未设置为对象的实例。
当我在不同的属性(邮政编码)上尝试相同的方法时,如果没有找到记录,它也可以正常工作。:
Location item = connection
.Table<Location>()
.Where(l => l.Postcode.Equals(label))
.FirstOrDefault();
这是位置类:
// These are the Locations where the Stock Take Sessions are done
public class Location : DomainModels, IComparable<Location>
{
[JsonProperty("id"), PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public int Number { get; set; }
public string Postcode { get; set; }
public string City { get; set; }
public bool Completed { get; set; }
[Ignore] // Removing this does not have an impact on the NullReferenceException
public string Label => $"{Name ?? ""} - ({Postcode ?? ""})";
public int CompareTo(Location other)
{
return Name.CompareTo(other.Name);
}
// Navigation property
// One to many relationship with StockItems
[OneToMany(CascadeOperations = CascadeOperation.All), Ignore]
public List<StockItem> StockItems { get; set; }
// Specify the foreign key to StockTakeSession
[ForeignKey(typeof(StockTakeSession))]
public int StockTakeSessionId { get; set; }
// One to one relationship with StockTakeSession
[OneToOne]
public StockTakeSession StockTakeSession { get; set; }
}
我做错了什么?
感谢您的任何建议!
【问题讨论】:
-
connection.Table<Location>中的Location之一具有Label= null。这就是为什么你有这个问题。您需要确保connection.Table<Location>中的所有项目都设置了 Label 属性。 -
@ChetanRanpariya:我检查了数据库,没有一条记录缺少姓名或邮政编码。
标签: c# android sqlite-net-extensions