Search code examples
coolstorage

Searching many-to-many relations with Vici CoolStorage


For an application used for managing booktitles and such, I'm using CoolStorage as an ORM.

The database has a table named 'titles'. Each title can have n authors, stored in the 'authors' table. The two tables are connected through the linktable 'titles_authors', which is pure.

When I try to filter titles on authors, I get an exception:

Unable to cast object of type 'QueryExpression' to type 'System.String'

Does anyone know how to filter the titles by author?


Solution

  • Set this up in your Title Class:

    [ManyToMany("titles_authors", Pure = true, ForeignLinkKey = "AuthorId", LocalLinkKey = "TitleId")]
    public abstract CSList<Author> Authors{ get; set; }
    

    And vice versa in the Authors class...

    Then you can access by:

    CSList titlesByAuthor = Title.List("Authour.Name = "Bob Thorn");