Table of contents
OneToOneAttribute API Document
Associates a foreign table where the current class and the target class share their primary key.
Quick Note
Usually classes that uses the primary key generated elsewhere (foreign) uses the PrimaryKey attribute with the generator type PrimaryKeyType.Foreign
Example
The following code exemplifies two classes that maps to two tables sharing the primary key:
[ActiveRecord("Employee")]
public class Employee : ActiveRecordBase
{
private int id;
private Award award;
[PrimaryKey(PrimaryKeyType.Native, "EmployeeID")]
public int ID
{
get { return this.id; }
set { this.id = value; }
}
[OneToOne]
public Award Award
{
get { return this.award; }
set { this.award = value; }
}
}
[ActiveRecord("Award")]
public class Award : ActiveRecordBase
{
private Employee employee;
private int id;
public Award()
{
}
public Award(Employee employee)
{
this.employee = employee;
}
[OneToOne]
public Employee Employee
{
get { return this.employee; }
set { this.employee = value; }
}
[PrimaryKey(PrimaryKeyType.Foreign, "EmployeeID")]
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public static Award[] FindAll()
{
return ((Award[]) (ActiveRecordBase.FindAll(typeof(Award))));
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll( typeof(Award) );
}
}
Employee emp = new Employee();
emp.Name = "john doe";
emp.Save();
Award award = new Award(emp);
award.Description = "Invisible employee";
award.Save();
Constructors
OneToOneAttribute()
Initializes a new instance of the Castle.ActiveRecord.OneToOneAttribute class.
Properties
| Name | Type | Description |
|---|---|---|
| MapType | Type | Allows one to reference a different type than the property type |
| Cascade | CascadeEnum | From NHibernate docs: specifies which operations should be cascaded from the parent object to the associated object. |
| Fetch | FetchEnum | From NHibernate docs: Chooses between outer-join fetching or sequential select fetching. |
| PropertyRef | String | From NHibernate docs: The name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used. |
| Constrained | Boolean | From NHibernate docs: specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which Save() and Delete() are cascaded (and is also used by the schema export tool). |
| Access | PropertyAccess | Gets or sets the access strategy for this property |
| CustomAccess | String | Gets or sets the custom access strategy |
| AccessString | String | Gets the access strategy string for NHibernate's mapping. |