我有两个简单的表格如下:
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; }
OR
public MediaPartner IdMediaPartner { get; set; }
does not compile.
我的问题是:有没有办法在这两个表之间建立一对多的关系?
谢谢!
解决方法
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插件支持,是跨平台的,可用于分叉和贡献.