MVVMCross社区SqLite - 表之间的关系

时间:2014-01-12 15:22:40

标签: sqlite mvvmcross sqlite-net

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

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

问题:

  

公开名单&lt; MediaPartner&gt; IdMediaPartner {get;组; }
  
      public MediaPartner IdMediaPartner {get;组; }
  不编译。

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

谢谢!

1 个答案:

答案 0 :(得分: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插件支持,是跨平台的,可用于分叉和贡献。