views:

21

answers:

1

I am trying to create a Unique Key on a class that includes the foreign key to its parent. However, I get the following error: "Could not determine type for Parent". Below is the class and Fluent NHibernate mapping. I get that it is unable to figure out an SQL type for the Parent class, since it is just a proxy for the ParentFk field, but not sure what more to do to facilitate the translation:

Class

   public class Item : BaseEntity
    {    
        #region Properties

        [DomainSignature]
        public virtual string ItemNumber { get; set; }

        [NotNull]
        [DomainSignature]
        public virtual Parent Parent { get; set; }

        [DomainSignature]
        public virtual long Iteration { get; set; }

        remainder elided....
    }

Mapping Override

public void Override(AutoMapping<Item> mapping)
{
    mapping.Map(t => t.ItemNumber).UniqueKey("UIX_Item_NaturalKey").Not.Nullable();
    mapping.Map(t => t.Iteration).UniqueKey("UIX_Item_NaturalKey").Not.Nullable();
    mapping.Map(t => t.Parent, "ParentFk").UniqueKey("UIX_Item_NaturalKey").Not.Nullable();
}

So, is there a way to create a Unique Key that includes a Foreign Key, and, in this case, given that I have the DomainSignature attribute on all of the fields anyway, is it necessary?

Thanks!

A: 

Well, based on this post, it seems that the DomainSignature attribute is supposed to enforce uniqueness. However, my experience is that it will not prevent you from saving a duplicate item. The implementaion of Equals only uses the Domain Signature fields for checking equality when both objects are transient, and it definitely doesn't do a look-up in your database to ensure that the object you're about to save isn't already there.

sydneyos

related questions