Unique key on multiple columns where one is a foreign key - how to ?
Hi,
I have a DictionaryElement class like:
public class DictionaryElement : BaseEntity
{
[DomainSignature]
public virtual string Alias { get; set; }
public virtual Dictionary Dictionary { get; set; }
public virtual string Name { get; set; }
}
I'm trying to set unique key on two colums: alias and DictionaryFk (conventions from SharpArch by default add "Fk" to foreign key columns).
I define automappings like:
public class DictionaryElementMap : IAutoMappingOverride
{
public void Override(AutoMapping<DictionaryElement> mapping)
{
const string uniqueForDictionaryKeyName = "uniqueForDictionary";
mapping.Map(element => element.Alias).Index(uniqueForDictionaryKeyName);
mapping.Map(element => element.Dictionary).Index(uniqueForDictionaryKeyName);
}
}
but that results in an error when I try to execute schema:
- Database was not configured through Database method.
----> NHibernate.MappingException : Could not compile the mapping document: (XmlDocument) ----> NHibernate.MappingException : Could not determine type for: Common.Data.Dictionaries.Dictionary, Common.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Dictionary)
And mappings (dumped to .hbm) look like:
<property name="Alias" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Alias" index="uniqueForDictionary" />
</property>
<property name="Dictionary" type="Common.Data.Dictionaries.Dictionary, Common.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="Dictionary" index="uniqueForDictionary" />
</property>
<property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
which is totaly wrong for "Dictionary".
I noticed that the same think works when I would like to set this unique key on two columns that are not foreign keys, like for example name and alias. then mapping looks like:
<column name="Alias" index="uniqueForDictionary" />
</property>
<property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" index="uniqueForDictionary" />
</property>
<many-to-one class="Common.Data.Dictionaries.Dictionary, Common.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Dictionary">
<column name="DictionaryFk" />
</many-to-one>
and is correct.
Can anybody help me with any suggestions?
thanks,
Łukasz Podolak
Support Staff 2 Posted by Paul Batum on 25 Sep, 2010 10:58 PM
Hi Lukasz,
Your DictionaryElement references a Dictionary object by foreign key, right?
Well you can't use the .Map method to map this... that tells FNH that its
just a simple property. Instead, use this:
References(x => x.Dictionary).Index(uniqueForDictionaryKeyName);
2010/9/24 Łukasz Podolak <
***@tenderapp.com<tender%***@tenderapp.com>
>
3 Posted by Łukasz Podolak on 27 Sep, 2010 12:23 PM
I'm an idiot,
Thanks for a fresh pair of eyes :)
Of course, what you said works.
Btw, for uniqueness I should use UniqueKey(string) rather than Index(string).