Well, a link is a link; one object has a link to another. The converse is not necessarily true.
In your case, you can get away with HasOne relationships. However, HasOne is commonly used for denormalized data. Say that you need additional information about the owner, but you cannot change the ownerβs scheme, because another code depends on it. You must create an AdditionalOwnerInfo object and create a table in the schema in which the OwnerID field of the table was the foreign key for the owner, as well as the primary key of the table.
Ayende recommends a two-way reference () in 99.9% of the one-to-one cases, where the second object is conceptually separate from the first, but there is an implicit type of "I own only one thing" relationship. You can enforce the "one and only one" character of a link using the Unique().Not.Nullable() set in the link mapping.
To streamline the reference setting, consider defining one object (UmbrellaOwner) as a "parent" and another (Umbrella) as a "child", and in the parent property installer set the parent parent element to the current link:
public class Umbrella { public virtual string ID { get; set; } public virtual Owner Owner { get; set; } } public class UmbrellaOwner { public virtual string ID { get; set; } private Umbrella umbrella; public virtual Umbrella Umbrella { get{ return umbrella; } set{ umbrella = value; if(umbrella != null) umbrella.Owner = this; } } }
Now, when you assign a child to a parent, the backreference function is automatically configured:
var owner = new UmbrellaOwner{Umbrella = new Umbrella()}; Assert.AreEqual(owner, owner.Umbrella.Owner);
Keiths
source share