“在创建SessionFactory时使用了无效或不完整的配置”NHibernate

时间:2015-01-31 03:57:18

标签: c# asp.net-mvc fluent-nhibernate

创建SessionFactory时出现此错误。这是代码,如果有人可以检查出来。

    class NHibernateHelper {

    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory {
        get {
            if (_sessionFactory == null) {
                InitializeSessionFactory();
            }
            return _sessionFactory;
        }
    }

    private static void InitializeSessionFactory() {
        _sessionFactory = Fluently.Configure().
            Database(MsSqlConfiguration.MsSql2008.ConnectionString
            ("Server=tcp:z4m56fgh.database.windows.net,1433;Database=db;User ID=user;Password=xxxxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;").
            ShowSql()).
            Mappings(m => m.FluentMappings.AddFromAssemblyOf<House>()).
            ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true)).
            BuildSessionFactory();
    }

    public static ISession OpenSession() {
        return SessionFactory.OpenSession();
    }
}

映射:

   public class HouseMap : ClassMap<House> {

    public HouseMap() {
        Id(x => x.Id);
        References(x => x.Owner).Cascade.All();
        References(x => x.Rooms).Cascade.All();
        References(x => x.Consumptions).Cascade.All();
    }
}

    public class ConsumptionMap : ClassMap<Consumption> {

    public ConsumptionMap() {
        Id(x => x.Id);
        Map(x => x.Type);
        Map(x => x.AvgDay);
        Map(x => x.AvgMonth);
        Map(x => x.AvgYear);
    }
}

   public class RoomMap : ClassMap<Room> {

    public RoomMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Number);
        Component<TemperatureData>(x => x.TemperatureData,
            t => {
                t.Map(x => x.TemperatureCurrent, "Current");
                t.Map(x => x.TemperatureSet, "Set");
            });
        Component<RoomFeatures>(x => x.Features,
            f => {
                f.Map(x => x.Shutters, "ShuttersUp");
                f.Map(x => x.Lights, "LightsOn");
            });
    }
}

这个确切的类NHibernateHelper可以与另一个模型一起使用,因此我的映射必定是错误的。此外,如果我从HouseMap中排除所有内容,即对参考文献的评论,它仍然无效。

1 个答案:

答案 0 :(得分:1)

在没有课程的情况下,很难意识到问题的根源是什么,尽管猜测是在消费属性。如果它是一个列表(就像它的名字一样),它应该用 HasMany 而不是引用进行映射。

此外,也许您可​​以使用 InnerException 附加堆栈跟踪。这可以给我们一个线索。