我正在尝试加载引用,但在这种情况下,来自同一个表的两个引用它不起作用
[Required]
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
[References(typeof(Employee))]
public int? CreatedById { get; set; }
[Reference]
public Employee CreatedBy { get; set; }
[References(typeof(Employee))]
public int? ModifiedById { get; set; }
[Reference]
public Employee ModifiedBy { get; set; }
正如您在上面所看到的,我以正确的方式做了它作为ORMLite的文档: https://github.com/ServiceStack/ServiceStack.OrmLite
即使属性ModifiedById和CreatedById具有值,也不会加载属性:ModifieldBy和CreatedBy。我尝试使用必填字段并且它有效,但是当我尝试使用int时? nulable它不起作用。
你可以在下面的图片中看到我有一个值为" 5"在ModifiedById列中,所以我的方法的Loadreferences应该得到列:ModifiedBy(加载了Employee对象)它在其他情况下工作,而不是在这种情况下。
答案 0 :(得分:2)
我添加了一个通过测试,用可空int?
ID in this commit来测试多个自引用:
使用的类型:
public class ParentSelfRef
{
[AutoIncrement]
public int Id { get; set; }
[References(typeof(ChildSelfRef))]
public int? Child1Id { get; set; }
[Reference]
public ChildSelfRef Child1 { get; set; }
[References(typeof(ChildSelfRef))]
public int? Child2Id { get; set; }
[Reference]
public ChildSelfRef Child2 { get; set; }
}
public class ChildSelfRef
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
}
删除并重新创建表格:
db.DropTable<ParentSelfRef>();
db.DropTable<ChildSelfRef>();
db.CreateTable<ChildSelfRef>();
db.CreateTable<ParentSelfRef>();
使用多个自引用创建记录:
var row = new ParentSelfRef
{
Child1 = new ChildSelfRef
{
Name = "Child 1"
},
Child2 = new ChildSelfRef
{
Name = "Child 2"
},
};
保存并测试填充的行:
db.Save(row, references: true);
row.PrintDump();
Assert.That(row.Id, Is.EqualTo(1));
Assert.That(row.Child1Id, Is.EqualTo(1));
Assert.That(row.Child1.Id, Is.EqualTo(1));
Assert.That(row.Child1.Name, Is.EqualTo("Child 1"));
Assert.That(row.Child2Id, Is.EqualTo(2));
Assert.That(row.Child2.Id, Is.EqualTo(2));
Assert.That(row.Child2.Name, Is.EqualTo("Child 2"));
使用自引用加载行:
row = db.LoadSingleById<ParentSelfRef>(row.Id);
Assert.That(row.Id, Is.EqualTo(1));
Assert.That(row.Child1Id, Is.EqualTo(1));
Assert.That(row.Child1.Id, Is.EqualTo(1));
Assert.That(row.Child1.Name, Is.EqualTo("Child 1"));
Assert.That(row.Child2Id, Is.EqualTo(2));
Assert.That(row.Child2.Id, Is.EqualTo(2));
Assert.That(row.Child2.Name, Is.EqualTo("Child 2"));