How to use SQLite-Net Extensions with Composite keys

拜拜、爱过 提交于 2019-12-02 23:08:16

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/26623146/how-to-use-sqlite-net-extensions-with-composite-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!