1

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

问题 如代码示例所示,[ManyToOne]属性用于表示关系字段。这不起作用。如BitBucket Developer Page所述,该[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; }
        }
    }
}
4

1 回答 1

1

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

作为替代方案,您可以使用中间表来存储关系,与ManyToMany关系的工作方式相同,但将一端限制为一个。通过这种方式,您可以使用 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作主键,则指向该类的外键也必须是 a Guid。在演示项目中,我们已经在使用int(性能最高的),string甚至UUID.

于 2015-10-21T15:44:22.207 回答