我正在使用 Xamarin 表单、SQLite.net 和 SQLitenet 扩展,但我无法弄清楚为什么我希望简单的东西不起作用。
我有两节课
public class MeasurementInstanceModel
{
public MeasurementInstanceModel ()
{
}
[PrimaryKey]
[AutoIncrement]
public int Id {
get;
set;
}
[ForeignKey(typeof(MeasurementDefinitionModel))]
public int MeasurementDefinitionId {
get;
set;
}
[ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
public MeasurementDefinitionModel Definition {
get;
set;
}
[ForeignKey(typeof(MeasurementSubjectModel))]
public int MeasurementSubjectId {
get;
set;
}
[ManyToOne(CascadeOperations = CascadeOperation.CascadeRead)]
public MeasurementSubjectModel Subject {
get;
set;
}
public DateTime DateRecorded {
get;
set;
}
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<MeasurementGroupInstanceModel> MeasurementGroups {
get;
set;
}
}
和
public class MeasurementSubjectModel
{
[PrimaryKey]
[AutoIncrement]
public int Id {
get;
set;
}
public string Name {
get;
set;
}
[OneToMany (CascadeOperations = CascadeOperation.All)]
public List<MeasurementInstanceModel> MeasurementInstances {get;set;}
}
我只是尝试执行以下查询,但它总是失败。
db.Table<MeasurementInstanceModel>().Where(w => w.Subject.Name == avariable);
我得到了这个例外
System.Diagnostics.Debugger.Mono_UnhandledException(ex={System.Reflection.TargetInvocationException:异常已被调用的目标抛出。---> System.NullReferenceException:对象引用未设置为 SQLite.Net 上的对象实例.TableQuery
1[MeasureONE.MeasurementInstanceModel].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 queryArgs) [0x00000] in :0 at SQLite.Net.TableQuery1[MeasureONE.MeasurementInstanceModel].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 queryArgs) [0x00000] in :0 at SQLite.Net.TableQuery1[MeasureONE.MeasurementInstanceModel].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 queryArgs) [0x00000] in :0 at SQLite.Net.TableQuery1[MeasureONE.MeasurementInstanceModel].GenerateCommand (System.String selectionList) [0x00000] in <filename unknown>:0 at SQLite.Net.TableQuery
1[MeasureONE .MeasurementInstanceModel].GetEnumerator () [0x00000] in :0 at System.Collections.Generic.List1[MeasureONE.MeasurementInstanceModel].AddEnumerable (IEnumerable
1 可枚举)[0x00000] in :0 at System.Collections.Generic.List1[MeasureONE.MeasurementInstanceModel]..ctor (IEnumerable
1 集合)[0x00000] in :0 在 System。 Linq.Enumerable.ToList[MeasurementInstanceModel] (IEnumerable1 source) [0x00000] in <filename unknown>:0 at MeasureONE.Repository
1[MeasureONE.MeasurementInstanceModel].GetAll[DateTime] (System.Linq.Expressions.Expression1 predicate, System.Linq.Expressions.Expression
1 orderBy, Nullable1 descending, Nullable
1 skip, Nullable1 count) [0x00094] in /Users/jean-sebastiencote/MeasureONE/MeasureONE/Models/Repository/Repository.cs:48 at MeasureONE.Repository
1[MeasureONE.MeasurementInstanceModel].GetAllWithChildren[DateTime] (System.Linq.Expressions.Expression1 predicate, System.Linq.Expressions.Expression
1 orderBy, Nullable1 descending, Nullable
1 个跳过,可空1 count) [0x00009] in /Users/jean-sebastiencote/MeasureONE/MeasureONE/Models/Repository/Repository.cs:54 at MeasureONE.MeasurementListViewModel.Load (System.Linq.Expressions.Expression
1 个预测,可空1 skip, Nullable
1 个计数)[0x00049] 在 /Users/jean-sebastiencote/MeasureONE/MeasureONE/ViewModels/MeasurementListViewModel.cs:42 在 MeasureONE.MeasurementListViewModel.Load(MeasureONE.FilterViewModel 过滤器)[0x000cf] 在 /Users/jean-sebastiencote/MeasureONE/ MeasureONE/ViewModels/MeasurementListViewModel.cs:34 at MeasureONE.MeasurementListViewModel.m__1 (GalaSoft.MvvmLight.Messaging.NotificationMessage`1 msg) [0x00007] in /Users/jean-sebastiencote/MeasureONE/MeasureONE/ViewModels/MeasurementListViewModel.cs:21 at (包装器托管到本机) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) 在 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System .Reflection.Binder 绑定器,System.Object[] 参数,System.Globalization。CultureInfoculture) [0x00000] in :0 --- 内部异常堆栈跟踪结束 ---
从堆栈跟踪中可以看出,我在代码中还有一些东西,例如 order by。但这一切都很好,只要我在子表上没有条件。