Setting Component as Unique Key
This may be a very simple question. I am running on Build# 650 of FluentNHibernate.
I am trying to set the Address component below as Unique key.
public class Employee
{
[DomainSignature]
public Address MailingAddress { get; set;}
}
public class Address
{
public string Street { get; set;}
public string City { get; set; }
}
I am trying to use IComponentConvention to do this and here is what I am doing:
public class ComponentConvention : IComponentConvention
{
public void Apply(IComponentInstance instance)
{
if (Attribute.IsDefined(instance.Property.MemberInfo, typeof(NaturalKeyAttribute)))
{
foreach (var property in instance.Properties)
{
property.UniqueKey("NaturalKey");
}
}
}
}
The problem is when I run the Mapping tests on Employee , I don't see the code entering ComponentConvention at all, so hence it is never translated as Unique keys in the database table. On the other hand, if I use an older build (Build# 633), I get the
code to enter ComponentConvention, but it doesn't have a settable UniqueKey. Am I doing something wrong here or is there a different approach to solve this problem.
Thanks,
sks
Support Staff 2 Posted by James Gregory on 16 May, 2010 04:53 PM
You're going to have to give us some more information. How are you mapping your classes? Automapping or ClassMap? Are you using ComponentMap to map your component?
3 Posted by sks on 17 May, 2010 04:18 PM
We are using ClassMap. Below is an example of our EmployeeMap.
public sealed class EmployeeMap : ClassMap
{
}
Here is our AddressMap.
public class AddressMap : ComponentMap
{
}
We added the below convention so that we can make the MailingAddress properties part of a unique constraint in the database if there
is a [DomainSignature] attribute on the MailingAddress component.
public class ComponentConvention : IComponentConvention
{
}
With the above convention, we are trying to get NHibernate to generate a unique constraint (DomainSignature) on Street and City in the Employee table.
Thanks,
sks
4 Posted by juggernaut78 on 02 Dec, 2010 09:33 PM
Is this a known issue for the IComponentConvention, that Apply is never called? I have same issue with FNH 1.1. Attaching simple repro solution for vstudio 2010.
Expected output from Tests\MappingTest.CanExportDbSchemaWithComponentPrefix :
a db schema output with prefixes for component address columns.
Actual output: no prefixes for the columns.
(Apply method is never called).