简体   繁体   English

如何:SQLite-Net扩展中的嵌套列表

[英]How to: Nested Lists in SQLite-Net Extensions

How to: Do nested lists in sqlite-net-extensions 如何:在sqlite-net-extensions中执行嵌套列表
Answer Found: Keeping the question as an example of how to do it. 找到答案:将问题作为如何做到的一个例子。
The problem i encountered was not sqlite-net-extensions related, but i'm keeping the question for context. 我遇到的问题不是sqlite-net-extensions相关,但我保留了上下文的问题。

[Old Question] I've got a problem with TwinCoders SQLite-net extensions. [旧问题] 我遇到了TwinCoders SQLite-net扩展的问题。
I'm trying to insert a Series object into my database: 我正在尝试将一个Series对象插入到我的数据库中:

I'm using the Db.InsertWithChildren(SelectedSeriesObject,recursive:true) method. 我正在使用Db.InsertWithChildren(SelectedSeriesObject,recursive:true)方法。
The Series object is added accordingly with it's attributes. 相应地添加Series对象的属性。
All the Episodes are added as well, no problems there. 所有的剧集都加入了,没有问题。

The problem is the BaseSeason. 问题是BaseSeason。
It will only insert one Season object, which is (for some reason) the last Season Object of the list of Seasons in the Series 它只会插入一个Season对象,这是(由于某种原因)系列中Seasons的列表的最后一个Season对象

public class BaseSeries : BaseMedia
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[Indexed]
public int ShowId { get; set; }

public string FirstAirDate { get; set; }
public string LastAirDate { get; set; }
public string Status { get; set; }

[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseSeason> Seasons { get; set; }
/// <summary>
/// TvShow = 0, Anime = 1
/// </summary>
public int SeriesType { get; set; }

}

public class BaseSeason
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[ForeignKey(typeof(BaseSeries))]
public int SeasonId { get; set; }

public int SeasonNumber { get; set; }
public int NumberOfEpisodes { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public string AirDate { get; set; }

[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseEpisode> Episodes { get; set; }    
[ManyToOne]
public BaseSeries BaseSeries { get; set; }

}

public class BaseEpisode
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

[ForeignKey(typeof(BaseSeason))]
public int EpisodeId { get; set; }

public string Title { get; set; }
public string Plot { get; set; }
public string Poster { get; set; } //still path
public string AirDate { get; set; }
public int EpisodeNumber { get; set; }
public int SeasonNumber { get; set; }
public string SeriesName { get; set; }    

[ManyToOne]
public BaseSeason BaseSeason { get; set; }
}

Is there anyone with experience regarding nested relationships in sqlite-net-extensions that knows how to make this work or see what i did wrong? 是否有任何人有关于sqlite-net-extensions中嵌套关系的经验,知道如何使这项工作或看到我做错了什么?

预期的关系

So regarding writing nested lists in sqlite-net-extensions: 所以关于在sqlite-net-extensions中编写嵌套列表:

My problem turned out the be related to how I handle the creation of these objects, this is by no means related to sqlite-net extensions. 我的问题与我如何处理这些对象的创建有关,这与sqlite-net扩展无关。 So my bad! 所以我的坏!

Which means that the questions example is valid and works. 这意味着问题示例有效且有效。 (I tested it of course) (我当然测试了它)

  • Setting up the entities for the database: 设置数据库的实体:
    The example shown in my question, with a Series class, Season class and Episode class, is the correct way of setting it up. 我的问题中显示的示例,包括Series类,Season类和Episode类,是设置它的正确方法。

  • Inserting into the database: 插入数据库:
    If you're wondering how to insert an object similar to my Series object (with nested lists), use: 如果您想知道如何插入类似于我的Series对象的对象(使用嵌套列表),请使用:
    db.InsertWitchChildren(yourObject, recursion: true)
    Here's an extended example: 这是一个扩展示例:

     public void AddSeries() { MediaDB.db.CreateTable<BaseSeries>(); MediaDB.db.CreateTable<BaseSeason>(); MediaDB.db.CreateTable<BaseEpisode>(); MediaDB.db.InsertWithChildren(SelectedSeries, recursion: true); } 

Side Note: 边注:
The example uses a static property on class with the connection string. 该示例在类上使用带有连接字符串的静态属性。 Like so: 像这样:

    public class MediaDB
    {
        public static SQLiteConnection db => new SQLiteConnection(new SQLitePlatformGeneric(),"Media.db");
    }

Refrain from doing this it is not really the best thing to do, since you should use using for the SQLiteConnection, making sure it's disposed once you're done with it. 从这样避免它是不是真的做的最好的事情,因为你应该使用using的SQLiteConnection,确保一旦你用它做它的配置。

more info on: sqlite-net-extentions 更多信息: sqlite-net-extentions

[UPDATE]: Further expansion of handling nested lists in sqlite-net extensions: [更新]:在sqlite-net扩展中进一步扩展处理嵌套列表:

  • Deleting tables with children: 删除带子项的表:
    This is quite simple, but i spent a good hour and half figuring it out anyways. 这很简单,但无论如何我花了一个半小时计算出来。
    Just use: 只需使用:
    For lists/arrays: db.DeleteAll(yourCollection, recursion: true) 对于列表/数组: db.DeleteAll(yourCollection, recursion: true)
    For single objects: db.Delete(yourObject, true); 对于单个对象: db.Delete(yourObject, true);
    As an exmaple: here's my implementation of a method that will delete a List 作为一个例子:这是我实现的一个删除List的方法
    (BaseSeries is the class shown in the original question question): (BaseSeries是原始问题中显示的类):

     public static void RemoveCollection<T>(List<T> collection) { using (db) { if (typeof(T) == typeof(BaseMovie)) { db.DeleteAll(collection); } if (typeof(T) == typeof(BaseSeries)) { db.DeleteAll(collection, recursion: true); } } } 

    The BaseMovie class is a simple single entity, recursion is not needed since it holds no children. BaseMovie类是一个简单的单个实体,因为它不包含子节点,所以不需要递归。

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

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