Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 218525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:45:33+00:00 2026-05-11T18:45:33+00:00

I’ve been trying for hours to get many-to-many relationship to save with Castle ActiveRecord.

  • 0

I’ve been trying for hours to get many-to-many relationship to save with Castle ActiveRecord. What am I doing wrong? I can’t find anything in the documentation or on google. There is data in the database.

Courses have a many to many relationship with Books.

Test code.

Database.Course c = new Database.Course();
c.Number = "CS 433";
c.Name = "Databases";
c.Size = 34;
c.Books = Database.Book.FindAll();
c.Save();

Also doesn’t work

foreach(Database.Book b in Database.Book.FindAll()){
    c.Books.Add(b);
}

Database Classes

[ActiveRecord]
public class Course : ActiveRecordValidationBase<Course>
{
    private int? id;
    private string number;
    private string name;
    private string description;
    private int size; //number of students in class

    //references
    private IList books = new ArrayList();


    public override string ToString()
    {
        return FormattedName;
    }

    public string FormattedName
    {
        get
        {
            return string.Format("{0} - {1}", Number, Name);
        }
    }

    [PrimaryKey]
    public int? Id
    {
        get { return id; }
        set { id = value; }
    }

    [Property, ValidateNonEmpty]
    public string Number
    {
        get { return number; }
        set { number = value; }
    }

    [Property, ValidateNonEmpty]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    [Property(ColumnType="StringClob")]
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    [Property]
    public int Size
    {
        get { return size; }
        set { size = value; }
    }

    [HasAndBelongsToMany(typeof(Book),
    Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id", Inverse = true)]
    public IList Books
    {
        get { return books; }
        set { books = value; }
    }
}

[ActiveRecord]
public class Book : ActiveRecordValidationBase<Book>
{
    private int? id;
    private string title;
    private string edition;
    private string isbn;
    private bool is_available_for_order;
    //relations
    private IList authors = new ArrayList();
    private IList bookordercount = new ArrayList();
    private IList courses = new ArrayList();
    private Inventory inventory;

    public override string ToString()
    {
        return FormattedName;
    }

    public string FormattedName
    {
        //*
        get {
            string str;
            if (Edition == null || Edition == "")
                str = Title;
            else
                str = string.Format("{0} ({1})", Title, Edition);

            if (Authors.Count != 0)
            {
                return string.Format("{0} by {1}", str, FormattedAuthors);
            }
            else
            {
                return str;
            }
        }
        /*/
        get
        {
            return Title;
        }
        //*/
    }

    public string FormattedAuthors
    {
        get
        {
            if (Authors.Count == 0) return "";

            StringBuilder sb = new StringBuilder();
            int i = 0, end = Authors.Count;
            foreach (Author a in Authors)
            {
                i++;
                sb.Append(a.FormattedName);
                if (i != end) sb.Append("; ");
            }
            return sb.ToString();
        }
    }


    [PrimaryKey]
    public int? Id
    {
        get { return id; }
        set { id = value; }
    }

    [Property, ValidateNonEmpty]
    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    [Property]
    public string Edition
    {
        get { return edition; }
        set { edition = value; }
    }

    [Property, ValidateNonEmpty]
    public string Isbn
    {
        get { return isbn; }
        set { isbn = value; }
    }

    [Property]
    public bool IsAvailableForOrder
    {
        get { return is_available_for_order; }
        set { is_available_for_order = value; }
    }

    //relations

    [HasAndBelongsToMany(typeof(Author),
    Table = "BookAuthor", ColumnKey = "book_id", ColumnRef = "author_id")]
    public IList Authors
    {
        get { return authors; }
        set { authors = value; }
    }

    [HasMany(typeof(BookOrderCount), Table = "BookOrderCounts", ColumnKey = "BookId")]
    public IList BookOrderCount
    {
        get { return bookordercount; }
        set { bookordercount = value; }
    }

    [HasAndBelongsToMany(typeof(Course),
   Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id")]
    public IList Courses
    {
        get { return courses; }
        set { courses = value; }
    }

    [OneToOne]
    public Inventory Inventory
    {
        get { return inventory; }
        set { inventory = value; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:45:33+00:00Added an answer on May 11, 2026 at 6:45 pm

    Make sure you put the Inverse = true where you want it. From the Castle AR docs,

    It is wise to choose one side of the
    relation as the owner. The other side,
    the non-writable, need to use
    Inverse=true.

    Put the Inverse = true on the other side of the relationship, like this:

    [HasAndBelongsToMany(typeof(Book),
        Table = "BookCourse", ColumnKey = "course_id", ColumnRef = "book_id")]
        public IList<Book> Books
    
    [HasAndBelongsToMany(typeof(Course),
       Table = "BookCourse", ColumnKey = "book_id", ColumnRef = "course_id", Inverse = true)]
        public IList<Course> Courses
    

    You also have to add attributes to the top of both classes – at the moment they don’t know what tables they’re mapped to. Currently you have this:

    public class Course : ActiveRecordBase<Course>
    

    Add this (where “course” is the name of your Course table):

    [ActiveRecord("course")]
    public class Course : ActiveRecordBase<Course>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.