Setting Component as Unique Key

sks's Avatar

sks

12 May, 2010 08:19 PM via web

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

  1. Support Staff 2 Posted by James Gregory on 16 May, 2010 04:53 PM

    James Gregory's Avatar

    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?

  2. 3 Posted by sks on 17 May, 2010 04:18 PM

    sks's Avatar

    We are using ClassMap. Below is an example of our EmployeeMap.

    public sealed class EmployeeMap : ClassMap
    {

    public EmployeeMap()
    {       
        Component(x => x.Address)
            .ColumnPrefix("Mailing");
    }
    

    }

    Here is our AddressMap.

    public class AddressMap : ComponentMap


    {

    public AddressMap()
    {
        Map(x => x.Street);
        Map(x => x.City);
    }
    

    }

    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
    {

    public void Apply(IComponentInstance instance) 
    { 
            if (Attribute.IsDefined(instance.Property.MemberInfo,typeof(DomainSignatureAttribute))) 
            { 
                    foreach (var property in instance.Properties) 
                    { 
                            property.UniqueKey("DomainSignature"); 
                    } 
            } 
    }
    

    }

    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

  3. 4 Posted by juggernaut78 on 02 Dec, 2010 09:33 PM

    juggernaut78's Avatar

    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).

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What number comes after 20?