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; }
}

问题:

公共列表< MediaPartner > IdMediaPartner { 获取;放; }

公共 MediaPartner IdMediaPartner { 获取;放; }
不编译。

我的问题是:有没有办法在这两个表之间建立一对多的关系?

谢谢!

4

1 回答 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 插件支持,是跨平台的并且可用用于分叉和贡献。

于 2014-01-12T17:10:36.357 回答