【发布时间】:2014-05-11 07:05:54
【问题描述】:
是否可以使用 ORMLite v4 for ServiceStack 自动保存具有嵌套 [Reference] 属性的对象?例如:
public class Patient
{
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
[Reference]
public List<Insurance> Insurances { get; set; }
}
public class Insurance
{
[PrimaryKey]
public int Id { get; set; }
[ForeignKey(typeof(Patient))]
public int PatientId { get; set; }
public string InsuranceName { get; set; }
public string InsuranceLevel { get; set; }
[Reference]
public List<Contact> InsuranceContacts { get; set; }
}
public class Contact
{
[PrimaryKey]
public int Id { get; set; }
[ForeignKey(typeof(Insurance))]
public int InsuranceId { get; set; }
public string ContactName { get; set; }
}
我很想能够做到这一点......
var patient = new Patient
{
Name = "Nathan",
Insurances = new List<Insurance>
{
new Insurance
{
InsuranceName = "Aetna",
InsuranceLevel = "Primary",
InsuranceContacts = new List<Contact>
{
new Contact
{
ContactName = "Bob"
}
}
},
new Insurance
{
InsuranceName = "BCBS",
InsuranceLevel = "Secondary",
InsuranceContacts = new List<Contact>
{
new Contact
{
ContactName = "Susan"
}
}
}
}
}
db.Save(patient, references:true);
...让它写入所有三个表。就目前而言,我能想到的最好的办法是在保存顶级对象后添加它(“references:true”确实保存了第一个嵌套级别的引用——也就是说,保险表已正确填充):
foreach(Insurance insurance in patient.Insurances)
{
dbConn.SaveAllReferences(insurance);
}
这对于依赖 [Reference] 表来存储和关联数据的深层嵌套 JSON 结构可能会很麻烦。有没有更好的办法?
谢谢!
【问题讨论】:
标签: c# .net servicestack ormlite-servicestack