Relationships

You can link tables on Jenga using the foreign key and reference attributes.

Sample Schema.

//Book Model Class
    [AutoGenerateTable(1)]
    [JengaEntity]
    [Alias("books")]
    public class Book
    {
        [PrimaryKey]
        [AutoIncrement]
        public int Id { get; set; } 
        [JengaIdentifier]
        public string Title { get; set; } 
        public string ISBN { get; set; }
        public string Author { get; set; }
        public string Edition { get; set; }
        public string Publication { get; set; }

        [JengaTextArea]
        public string Notes { get; set; }
        public DateTime CreatedAt { get;} = DateTime.Now;

        [Reference] 
        public List<BookCopy> Copies { get; set; } = new List<BookCopy>();
    }
// Book Copy
    [AutoGenerateTable(2)]
    [JengaEntity]
    [Alias("book_copies")]
    public class BookCopy
    {
        [PrimaryKey]
        [AutoIncrement]
        public int Id { get; set; }

        [ForeignKey(typeof(Book), OnDelete = "CASCADE")]
        public int BookId { get; set; }
        [Reference]
        public Book Book { get; set; }

        [JengaIdentifier]
        public string BarCode { get; set; }
 
        public DateTime CreatedAt { get;}
 
    }

One to one

Fetch book info of a copy.


        [ForeignKey(typeof(Book), OnDelete = "CASCADE")]
        public int BookId { get; set; }
        [Reference]
        public Book Book { get; set; }//will automatically be mapped

One to many

Fetch many copies of one book

 [Reference]
 public List<BookCopy> Copies { get; set; } = new List<BookCopy>();

If the column is enabled for table. Jenga will automatically link the record relationships and replace it with [jengaIdentifier] column.

Last updated