Search code examples
c#sqlitesqlite-net-extensions

SQLite-Net Extension and FileModel


I want make a class representing file/folder, which has one or none parent and can contain files/folders. Because My folder is special case of file. My model looks like this:

namespace WPP3.Base.DataModel
{
  public class DbFileModel
  {
    [PrimaryKey]
    public string Path { get; set; }

    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
    public string DisplayName { get; set; }
    public bool isFile { get; set; }

    [ForeignKey(typeof(DbFileModel))]
    public string ParentID { get; set; }

    [OneToMany(null, null, CascadeOperations = CascadeOperation.All)]
    public List<DbFileModel> Files { get; set; }

    [ManyToOne(null, null, CascadeOperations = CascadeOperation.All)]
    public DbFileModel ParentFile { get; set; }
  }
}

But when I am trying to use this class, SQLite-NetExtension throws this exception: "DbFileModel.Files: OneToMany inverse relationship shouldn't be List or Array".

Can you advise how make that class using SQLiteNetExtensions?

Thank you very much :-)


Solution

  • On relationships to same class, you have to explicitly declare inverse relationships like this:

    [OneToMany(inverseProperty: "ParentFile", CascadeOperations = CascadeOperation.All)]
    public List<DbFileModel> Files { get; set; }
    
    [ManyToOne(inverseProperty: "Files", CascadeOperations = CascadeOperation.All)]
    public DbFileModel ParentFile { get; set; }
    

    There's a pending issue that would improve the discovery of inverse relationships so in future releases this may be no longer necessary.