2

I want to hide the primary key Id property from consumers of my entity classes:

public class A
{
    protected virtual int Id { get; set; }
    public virtual string Name { get; set; }
    ... etc ...
}

Making the Id property protected doesn't work with the standard Automapping, it fails to find it.

I tried overriding DefaultAutomappingConfiguration.IsId(...) but this only gets called back with public members.

How can I get this to work without using specific ClassMap<A>s for each type as documented here: http://wiki.fluentnhibernate.org/Fluent_mapping_private_properties

EDIT: I want to change the automapping conventions to look for any property with the name 'Id', not just public properties. I do not want to configure it on a per-class basis using ClassMap<T> as follows:

public ClassAMap: ClassMap<A>
{  
    public ClassAMap()  
    {  
        Id(Reveal.Member<ClassAMap>("Id"));  
    }  
}
public ClassBMap: ClassMap<B>
{  
    public ClassBMap()  
    {  
        Id(Reveal.Member<ClassBMap>("Id"));  
    }  
}
... etc ...
Tyson
  • 14,726
  • 6
  • 31
  • 43

1 Answers1

0

If you are using automapping implement the IIdConvention Interface

public class PrimaryKeyConvention : IIdConvention
{
  public void Apply(IIdentityInstance instance)
  {
    instance.Column(instance.EntityType.Name + "Id");
  }
}

Or override the default auto-mapping as following:

public ProductMap : ClassMap<Product>
{  
  public ProductMap()  
  {  
    Id(Reveal.Member<Product>("Id"));  
  }  
}
Yaman
  • 1,030
  • 17
  • 33
  • I'm new to Fluent NHibernate, so correct me if I'm wrong, but your first suggestion, using the `IIdConvention`, isn't that used to setup conventions of how your properties map into database columns? It has nothing to do with identifying those properties to begin with? And the second option will work yes, but as I said in the question, I don't want to use per-class `ClassMap` setup, I want a convention driven approach that applies to all classes. – Tyson Feb 22 '12 at 14:09
  • I thought you can reveal the member when implementing IIdConvention – Yaman Feb 23 '12 at 14:50