I'm looking for a FluentNH (Fluent NHibernate) convention or configuration that ignores all properties that have no setter:
It would still map these:
public class foo{
public virtual int bar {get; private set;}
}
And omit these:
public class foo{
public virtual int fizz{get;private set;}
public virtual int bar{get {return fizz;}} //<-------
}
You should use a custom mapping configuration
public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
return member.CanWrite;
}
}
Usage :
var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();
var.fluentConfiguration = Fluently.Configure(nhConfiguration );
.Mappings(m => m.AutoMappings.Add(
AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
));
var sessionFactory = this.fluentConfiguration.BuildSessionFactory();
However, private setters won't get mapped. You should get them as protected
Use this:
public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.IsProperty && !member.CanWrite)
{
return false;
}
return base.ShouldMap(member);
}
}
That should handle the case of no setter and private setter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With