fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

Allow specifiying attributes for NaturalId Property

Open MichaelLogutov opened this issue 13 years ago • 3 comments

I want to map this simple class:

public class Country
{
    public virtual int Id { get; protected set; }
    public virtual string Code { get; protected set; }
    public virtual string Name { get; protected set; }
}

The "Code" property is actually unique so I thought to use it as natural id. But I wanted to specify the length of it. So I've created this map:

public class CountryMap : ClassMap<Country>
{
    public CountryMap ()
    {
        this.Id (x => x.Id);

        this.NaturalId ().Property (x => x.Code);
        this.Map (x => x.Code).Length (2).Unique ();

        this.Map (x => x.Name).Length (255);
    }
}

And I got this exception: NHibernate.MappingException: Duplicate property mapping of Code found in Domain.Model.Geo.Country

The resulting HBM is this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="false" default-cascade="all">
  <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-update="true" mutable="false" optimistic-lock="none" name="Domain.Model.Geo.Country, Parts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Country`">
    <cache usage="read-only" />
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <natural-id>
      <property name="Code" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Code" />
      </property>
    </natural-id>
    <property name="Code" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Code" length="2" unique="true" />
    </property>
    <property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" length="255" />
    </property>
  </class>
</hibernate-mapping>

Packages I use:

<package id="FluentNHibernate" version="1.3.0.727" />
<package id="Iesi.Collections" version="3.2.0.4000" />
<package id="NHibernate" version="3.3.0.4000" />
<package id="NHibernate.Caches.SysCache" version="3.2.0.4000" />

MichaelLogutov avatar Jun 02 '12 06:06 MichaelLogutov

I have a similar problem. After having specifiec .UseOverridesFromAssemblyOf I am then using IAutoMappingOverride. When trying to specify that a string column is a NaturalId.ReadOnly I get the same error.

rohancragg avatar Jun 12 '12 14:06 rohancragg

It occurred to me to try using IgnoreProperty in the Override first...

This seems to prevent the error message (though the .ReadOnly() still seems not to be having the expected effect - CustomerCode column is still nullable)

public class CustomerConfigOverride: IAutoMappingOverride<Customer>
{
    public void Override(AutoMapping<Customer> mapping)
    {
        mapping.Schema("MySchema");
        mapping.IgnoreProperty(c => c.CustomerCode);
        mapping.NaturalId()
               .Property(c => c.CustomerCode)
               .ReadOnly();
...

rohancragg avatar Jun 12 '12 14:06 rohancragg

Same thing here, would be great if a ".Unique" was added to ".Property"

silversens avatar Dec 13 '12 09:12 silversens