BelongsTo
InitializingAndConfiguring.zip (15.81k)
A many-to-one relation can be mapped using the BelongsToAttribute
Consider the following table script:
CREATE TABLE Blogs ( [id] [int] IDENTITY (1, 1) NOT NULL, [name] [varchar] (50) NULL ) ON [PRIMARY] CREATE TABLE Posts ( [id] [int] IDENTITY (1, 1) NOT NULL, [title] [varchar] (50) NULL, [contents] [text] NULL, [blogid] [int] NULL ) ON [PRIMARY]
The blogid on Posts is clear a foreign key to the Blogs table. You can map the reference on the Post to a Blog, and to do it you can use BelongsToAttribute:
using Castle.ActiveRecord; [ActiveRecord("posts")] public class Post : ActiveRecordBase { private int id; private string title; private string contents; private Blog blog; [PrimaryKey] public int Id { get { return id; } set { id = value; } } [Property] public string Title { get { return title; } set { title = value; } } [Property(ColumnType="StringClob")] public string Contents { get { return contents; } set { contents = value; } } [BelongsTo("blogid")] public Blog OwnerBlog { get { return blog; } set { blog = value; } } }
Assigning a blog instance to this property - and obviously saving the post instance - will create the association.
More information on the attribute can be found at Attributes article.