简体   繁体   中英

How to: Nested Lists in SQLite-Net Extensions

How to: Do nested lists in 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.

[Old Question] I've got a problem with TwinCoders SQLite-net extensions.
I'm trying to insert a Series object into my database:

I'm using the Db.InsertWithChildren(SelectedSeriesObject,recursive:true) method.
The Series object is added accordingly with it's attributes.
All the Episodes are added as well, no problems there.

The problem is the 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

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?

预期的关系

So regarding writing nested lists in 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. 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.

  • Inserting into the database:
    If you're wondering how to insert an object similar to my Series object (with nested lists), use:
    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.

more info on: sqlite-net-extentions

[UPDATE]: Further expansion of handling nested lists in sqlite-net extensions:

  • 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)
    For single objects: db.Delete(yourObject, true);
    As an exmaple: here's my implementation of a method that will delete a List
    (BaseSeries is the class shown in the original question question):

     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.

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