MVVMCross社区SqLite - 表之间的关系

4

我有两个简单的表格如下:

public class MediaPartner
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string PhoneNumber { get; set; }
    public string CompanyName { get; set; }
    public double Lat { get; set; }
    public double Lng { get; set; }
    public DateTime InsertedUtc { get; set; }
}    

public class ImageGroup
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public List<MediaPartner> IdMediaPartner { get; set; }
    public string ImagePath { get; set; }
    public bool IsSent { get; set; }
    public DateTime InsertedUtc { get; set; }
}
问题:

public List< MediaPartner > IdMediaPartner { get; set; }
或者
public MediaPartner IdMediaPartner { get; set; }
无法编译。

我的问题是:是否有一种方法可以在这两个表之间建立一对多关系?
谢谢!
1个回答

6

SQLite-net只提供类似于索引的跨表引用方式:

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Indexed]
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }
}

sqlite-net至少有一个扩展允许声明OneToMany属性-请参见https://bitbucket.org/twincoders/sqlite-net-extensions,该扩展可以启用以下代码:

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Stock))]     // Specify the foreign key
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }

    [ManyToOne]      // Many to one relationship with Stock
    public Stock Stock { get; set; }
}

我不确定这个实现的确切方式 - 例如,我不知道是否使用了真正的FOREIGN KEY约束 - 但是代码是开源的,正在积极开发中,具有内置的mvvmcross插件支持,跨平台,并且可以进行分叉和贡献。


感谢您的回答和MVVMCross! - stephnx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接