简体   繁体   English

如何使用带有复合键的SQLite-Net扩展

[英]How to use SQLite-Net Extensions with Composite keys

I have the following class: 我有以下课程:

Class1.cs: Class1.cs:

[JsonObject(MemberSerialization.OptIn)]
public class Class1
{
    [PrimaryKey]
    [JsonProperty("key1")]
    public string Key1 { get; set; }

    [PrimaryKey]
    [JsonProperty("key2")]
    public string Key2 { get; set; }

    [PrimaryKey]
    [JsonProperty("key3")]
    public string Key3 { get; set; }

    [JsonProperty("normalStuff")]
    public string NormalStuff{ get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    [JsonProperty("customObjectList")]
    public List<CustomObject> CustomObjects { get; set; }
}

And then my CustomObject class: 然后是我的CustomObject类:

[JsonObject(MemberSerialization.OptIn)]
public class CustomObject
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Class1))]
    public string Class1Key{ get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}

Basically what I'm trying to accomplish is have a table that contains mappings to these custom objects. 基本上,我要完成的工作是有一个表,其中包含到这些自定义对象的映射。 Is this even supported in SQLite-Net extensions? SQLite-Net扩展是否甚至支持此功能? The composite keys aspect is supported in the newest version of the MVVMCross SQLite community package. MVVMCross SQLite社区软件包的最新版本支持组合键方面。 When I try to save the above data structure to my DB it saves everything except for my CustomObjects... those end up being null. 当我尝试将上述数据结构保存到数据库中时,它将保存除CustomObjects之外的所有内容……这些最终为null。 Let me know if you need any more information to understand what I'm trying to accomplish! 如果您需要更多信息以了解我要完成的工作,请告诉我!

First, thank you for your contribution to MvvmCross SQLite Community plugin. 首先,感谢您对MvvmCross SQLite社区插件的贡献。

Sadly, there's currently no support for composite keys in SQLite-Net Extensions, only MvvmCross implementation of SQLite-Net supports multiple primary keys and it's a very recent change. 遗憾的是,当前SQLite-Net扩展中不支持复合键,只有MvvmCross SQLite-Net的实现支持多个主键,这是最近的更改。

I'll take a look at the changes required to support it, but it will take some time. 我将研究支持它所需的更改,但这需要一些时间。 SQLite-Net Extensions was built on top of the assumption that primary keys were unique and therefore foreign keys would be unique per relationship too. SQLite-Net扩展建立在以下假设之上:主键是唯一的,因此每个关系的外键也将是唯一的。 This adaptation would require some deep changes in the architecture and the API. 这种调整将需要对体系结构和API进行一些深刻的更改。

I'm thinking in something like this to avoid changes in currently working public API: 我在想这样的事情,以避免更改当前正在使用的公共API:

[CompositeForeignKey(typeof(Class1), "key1", "key2", "key3"]
public Tuple<int, int, int> Class1Key { get; set; }

I've created a new issue in the project to keep track of the status. 我在项目中创建了一个新问题来跟踪状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM