FluentNhibernate + private set

2019-07-06 17:21发布

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

3条回答
对你真心纯属浪费
2楼-- · 2019-07-06 17:31

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-06 17:48

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.

查看更多
ら.Afraid
4楼-- · 2019-07-06 17:50

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; }
}
查看更多
登录 后发表回答