Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentNhibernate + private set

I'm using auto property with private set, and fluentNhibernate throw an error for me...

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. * Database was not configured through Database method.

This is my class:

public class MyClass
{
    public virtual int Id { get; set; }
    public virtual string PropOne { get; private set; } 
}

This is my map:

public class MyClassMap : ClassMap<MyClass>
{
    public MyClassMap()
    {
        Id(x => x.Id);
        Map(x => x.PropOne);
    }
}

If I change my propertie to:

public virtual string PropOne { get; protected set; }, 

the FN work fine.

But I read this topic: https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping "Access Strategies",and I've been making just like this topic. Where I wrong?

I put an example in GitHub: https://github.com/wbaldanw/NhAccessStrategies

Below, the code of BuildSession

    Configuration = new Configuration().Configure();
        var fluentConfiguration = Fluently.Configure(Configuration)
            .Mappings(x => x.FluentMappings.AddFromAssemblyOf<MyClassMap>());
        try
        {
            NHSession = fluentConfiguration.BuildSessionFactory();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
like image 946
Wesley B Avatar asked Feb 01 '26 01:02

Wesley B


2 Answers

I put an issue on FluentNhibernate project, and the correct is use private set with fields. If using autoproperties right is to use non-private setter.

This work fine:

private string name;

public string Name
{
  get { return name; }
}
like image 96
Wesley B Avatar answered Feb 03 '26 15:02

Wesley B


According to this question and answer it appears that this access strategy is no longer supported in NHibernate as of v. 3.3. The docs that you link to led me astray as well. They should probably be updated to note that this scenario is not supported after NHibernate 3.2.

like image 25
Michael Richardson Avatar answered Feb 03 '26 15:02

Michael Richardson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!