加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MVVMCross社区SqLite – 表之间的关系

发布时间:2020-12-12 18:56:33 所属栏目:百科 来源:网络整理
导读:我有两个简单的表格如下: 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 {
我有两个简单的表格如下:

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插件支持,是跨平台的,可用于分叉和贡献.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读