如何将外键映射到SQLite-Net Extensions中的对象字段

本文关键字:Extensions 对象 字段 SQLite-Net 映射 | 更新日期: 2023-09-27 18:18:37

上下文:我们正在使用 SQLite-Net Extensions 进行本地数据缓存和 Xamarin .我们计划部署到iOS,Android和Windows Phone。我们在整个系统中使用了现有的数据结构(都实现了通用接口(,我们希望以这种方式存储这些结构。

问题如代码示例中所示,[ManyToOne] 属性用于表示关系字段。这行不通。如 BitBucket 开发人员页面所述,[ForeignKey] 属性可用于指定外键关系。这似乎只支持int.我们是否可以轻松地调整我们的结构以支持这些关系,而无需重复 Id 字段的属性。例如,以下内容是不可取的。

    [ForeignKey(typeof(Address))]
    public int AddressId { set; get; }
    [ManyToOne]
    public Address Address
    {
        set { address = value; }
        get { return address; }
    }

代码示例

using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace Data
{
    [Table("Client")]
    public class Client : IData
    {
        private int id = -1;
        private Address address = null;
        public Client() { }
        public Client(int id)
        {
            this.id = id;
        }
        [PrimaryKey, AutoIncrement, Column("_id")]
        public int Id
        {
            set { id = value; }
            get { return id; }
        }
        [ManyToOne]
        public Address Address
        {
            set { address = value; }
            get { return address; }
        }
    }
    [Table("Address")]
    public class Address : IIdentifiable
    {
        private int id = -1;
        private string someFields = "";
        public Address() { }
        public Address(int id)
        {
            this.id = id;
        }
        [PrimaryKey, AutoIncrement, Column("_id")]
        public int Id
        {
            set { id = value; }
            get { return id; }
        }
        public string SomeFields
        {
            set { someFields = value; }
            get { return someFields; }
        }
    }
}

如何将外键映射到SQLite-Net Extensions中的对象字段

SQLite-Net Extensions 是 SQLite-Net 上的一层薄层,它使用 sqlite 数据库进行存储。关系数据库使用外键存储关系,sqlite 在这方面也不例外。因此,SQLite-Net 和 SQLite-Net 扩展也使用外键机制来声明关系。

作为替代方法,您可以使用中间表来存储关系,就像ManyToMany关系的工作方式一样,但将其中一个端限制为一个。这样,您就可以使用多对多关系和中间表来模拟OneToManyManyToOne甚至OneToOne关系。例如:

[Table("Client")]
public class Client {
    [PrimaryKey, AutoIncrement, Column("_id")]
    public int Id { get; set; }
    [Ignore] // This property shouldn't be persisted
    public Address Address { get; set; }
    // This relationship is in fact a ManyToOne relationship,
    // but we model it as a ManyToMany to avoid adding foreign key to this entity
    [ManyToMany(typeof(AddressesClients))]
    public Address[] Addresses { 
        get { return Address != null ? new []{ Address } : Address; } 
        set { Address = value.FirstOrDefault(); }
    }
}
[Table("Address")]
public class Address
{
    [PrimaryKey, AutoIncrement, Column("_id")]
    public int Id { get; set; }
    public string SomeFields { get; set; }
    [ManyToMany(typeof(AddressesClients), ReadOnly = true)]
    public List<Client> Clients { get; set; }
}
// Intermediate table that defines the relationship between Address and Client
class AddressesClients {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [ForeignKey(typeof(Client))]
    public int ClientId { get; set; }
    [ForeignKey(typeof(Address))]
    public int AddressId { get; set; }
}

当然,这会有一些性能损失。

至于PrimaryKey,你可以使用任何支持的类型,并且你必须对相反的ForeignKey使用完全相同的类型,即如果你使用Guid作为主键,指向该类的外键也必须是Guid在演示项目中,我们已经使用了int(性能最高(、string甚至UUID