Zero or more to many relationship

Issue #48 closed
Luke M created an issue

How can I accomplish this? I have files that can be inside a folder, or not (such as root files), and folders can be sub-folders with a parent folder, or root folders themselves. So I need 2 relationships of ZeroOrMoreToMany.

The file class is:

public class UserFile { [PrimaryKey, AutoIncrement] public int ID { get; set; }

[ManyToOne(inverseProperty: "Files")]
public UserFolder Folder { get; set; }

[ForeignKey(typeof(UserFolder))]
public int? FolderID { get; set; }

}

And the folder class is:

public class UserFolder { [PrimaryKey, AutoIncrement] public int ID { get; set; }

[ManyToOne(inverseProperty: "SubFolders")]
public UserFolder ParentFolder { get; set; }

[OneToMany(inverseProperty: "ParentFolder")]
public List<UserFolder> SubFolders { get; set; }

[OneToMany(inverseProperty: "Folder")]
public List<UserFile> Files { get; set; }

}

Comments (2)

  1. Guillermo GutiƩrrez

    Please ask this kind of questions in StackOverflow as described in the project description.

    ZeroOrMoreToMany is OneToMany. The zero is always implied.

    You are missing the parent folder ID foreign key. It should look like this

    public class Folder {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
    
        [ForeignKey(typeof(Folder))]
        public int ParentID { get; set; }
    
        [OneToMany(inverseProperty: "Parent")]
        public List<Folder> SubFolders { get; set; }
    
        [ManyToOne(inverseProperty: "SubFolders")]
        public Folder Parent { get; set; }
    
        [OneToMany]
        public List<File> Files { get; set; }
    }
    
    public class Folder {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
    
        [ForeignKey(typeof(Folder))]
        public int ParentID { get; set; }
    
        [ManyToOne]
        public Folder Parent { get; set; }
    }
    
  2. Log in to comment