FluentNhibernate + private set

时间秒杀一切 提交于 2020-01-14 14:33:11

问题


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



来源:https://stackoverflow.com/questions/20588293/fluentnhibernate-private-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!