FluentNhibernate + private set

2019-07-06 17:13发布

问题:

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

回答1:

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; }
}


回答2:

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.



回答3:

This might be a bug in FluentNH throwing misleading exception, but this exception is not related to the mapping itself, rather to building SessionFactory. Show us the code please, make sure you're setting DB driver & it's configuration via .Database(..) call