简体   繁体   中英

How to use SQLite-Net Extensions with Composite keys

I have the following class:

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:

[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? The composite keys aspect is supported in the newest version of the MVVMCross SQLite community package. When I try to save the above data structure to my DB it saves everything except for my CustomObjects... those end up being 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.

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.

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. This adaptation would require some deep changes in the architecture and the API.

I'm thinking in something like this to avoid changes in currently working public 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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