ORMLite 映射参考别名列

ORMLite Mapping reference Alias column

我将以下代码用于我的 POCO:如您所见,我的 属性 即我的参考被分配了一个别名。

    public class MasterItemAlias
{
    [PrimaryKey]
    public long ID { get; set; }

    [Reference]
    public MasterItem MasterItem { get; set; }           
    [Alias("MasterItem_id")]
    public int MasterItemId { get; set; } 

}

我使用 LoadSelect 进行查询,但只填充了 MasterItemID (int) 而不是引用对象 MasterItem。

var res = Db.LoadSelect<Resources.Entities.MasterItemAlias>();

issue with your example是因为Foreign Key Id和Primary Key类型不匹配,把MasterItemId改成long就可以了,例如:

public class MasterItem 
{
    [AutoIncrement]
    public long Id { get; set; }

    public string Code { get; set; }
}

db.CreateTable<MasterItem>();

public class MasterItemAlias
{
    [AutoIncrement] //missing as Id was not specified on Save below
    public long Id { get; set; }
    public string AliasCode { get; set; }
    [Reference]
    public MasterItem MasterItem { get; set; }           
    [Alias("MasterItem_id")]
    public long MasterItemId { get; set; } 
}

db.CreateTable<MasterItemAlias>();

var newMasterItem = new MasterItem {
    Code = "MI_CODE"
};
db.Save(newMasterItem);

var y = db.LoadSelect<MasterItemAlias>();

Note: you only need [AutoIncrement] which also makes it the Primary Key