Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifiying attributes for NaturalId Property #141

Open
MichaelLogutov opened this issue Jun 2, 2012 · 3 comments
Open

Allow specifiying attributes for NaturalId Property #141

MichaelLogutov opened this issue Jun 2, 2012 · 3 comments

Comments

@MichaelLogutov
Copy link

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" />
@rohancragg
Copy link

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
Copy link

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

@silversens
Copy link

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

@hazzik hazzik changed the title NaturalId and Map.Unique result in duplicate property mapping Allow sepcifiying attributes for NaturalId Property Jun 7, 2023
@gliljas gliljas changed the title Allow sepcifiying attributes for NaturalId Property Allow specifiying attributes for NaturalId Property Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants