NHibernate 类映射 = 0

发布于 2024-10-03 12:05:32 字数 2785 浏览 3 评论 0原文

我正在尝试使用 NHibernate / FluentNHibernate 在我的数据库中创建一个表。我似乎已经弄清楚了大部分内容,但是当我运行测试时,表没有创建。我在 Configuration 对象中看到 ClassMappings 是一个大零,即使我让用户 FluentNHibernate 从程序集中配置它们。我有点理解这一点,但我在某处缺少一些连接...这是代码片段,也许有人可以看到我忘记的内容?

这是我的 dataconfig 类。

public static FluentConfiguration GetFluentConfiguration()
    {
        string hibernateCfgFile = @"C:\Users\kenn\Documents\Visual Studio 2008\Projects\NHibernateTestTwo\Infrastructure\hibernate.cfg.xml";
        return Fluently.Configure(new Configuration().Configure(@hibernateCfgFile))
            .Mappings(cfg => cfg.FluentMappings.AddFromAssembly(typeof(AddressMap).Assembly));
    }

这是测试类。

[Test, Explicit]
    public void SetupDatabase()
    {
        FluentConfiguration conf = DataConfig.GetFluentConfiguration();
        conf.ExposeConfiguration(BuildSchema).BuildSessionFactory();
    }

    private static void BuildSchema(Configuration conf)
    {
        new SchemaExport(conf).SetOutputFile("drop.sql").Drop(false, true);
        new SchemaExport(conf).SetOutputFile("create.sql").Create(false, true);
    }

这是映射

 public AddressMap()
    {

        Table("Address");
        DynamicUpdate();
        Id(a => a.Id).GeneratedBy.GuidComb();
        Map(a => a.AddressOne).Not.Nullable().Length(100);
        Map(a => a.AddressTwo).Length(100);
        Map(a => a.City).Not.Nullable().Length(100);
        Map(a => a.state).Not.Nullable().Length(100);
        Map(a => a.zip).Not.Nullable().Length(50);
        Map(a => a.Primary).Not.Nullable();

    }

hibernate.cfg.xml 文件

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
    <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
        Data Source=MYPC;Initial Catalog=NHibernateSample;Integrated Security=True;
    </property>
    <property name="show_sql">true</property>
    <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
    </property>
    <property name="adonet.batch_size">100</property>
    <!--<property name="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
        NHibernate.ByteCode.LinFu
    </property>-->
    <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, 
        NHibernate.ByteCode.Castle
    </property>
</session-factory>

我只是不确定那里缺少什么...它显然正在与数据库交谈,因为如果我将数据库的名称更改为不存在的名称,它会引发异常,我被困住了 - 我已经在这个问题上绕了一圈又一圈,只是还没有弄清楚,所以任何帮助将不胜感激。

谢谢!

I am trying to user NHibernate / FluentNHibernate to create a table in my database. I seem to have figured it out for the most part but when I run the test the table isn't created. I see in the Configuration object that the ClassMappings is a big fat zero even thought I have user FluentNHibernate to configure them from an assembly. I somewhat understand this but I am missing some connection somewhere... Here is the code snippets, maybe someone can see what I forogt?

Here is my dataconfig class.

public static FluentConfiguration GetFluentConfiguration()
    {
        string hibernateCfgFile = @"C:\Users\kenn\Documents\Visual Studio 2008\Projects\NHibernateTestTwo\Infrastructure\hibernate.cfg.xml";
        return Fluently.Configure(new Configuration().Configure(@hibernateCfgFile))
            .Mappings(cfg => cfg.FluentMappings.AddFromAssembly(typeof(AddressMap).Assembly));
    }

Here is the test class.

[Test, Explicit]
    public void SetupDatabase()
    {
        FluentConfiguration conf = DataConfig.GetFluentConfiguration();
        conf.ExposeConfiguration(BuildSchema).BuildSessionFactory();
    }

    private static void BuildSchema(Configuration conf)
    {
        new SchemaExport(conf).SetOutputFile("drop.sql").Drop(false, true);
        new SchemaExport(conf).SetOutputFile("create.sql").Create(false, true);
    }

Here is the mappings

 public AddressMap()
    {

        Table("Address");
        DynamicUpdate();
        Id(a => a.Id).GeneratedBy.GuidComb();
        Map(a => a.AddressOne).Not.Nullable().Length(100);
        Map(a => a.AddressTwo).Length(100);
        Map(a => a.City).Not.Nullable().Length(100);
        Map(a => a.state).Not.Nullable().Length(100);
        Map(a => a.zip).Not.Nullable().Length(50);
        Map(a => a.Primary).Not.Nullable();

    }

The hibernate.cfg.xml file

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
    <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
        Data Source=MYPC;Initial Catalog=NHibernateSample;Integrated Security=True;
    </property>
    <property name="show_sql">true</property>
    <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
    </property>
    <property name="adonet.batch_size">100</property>
    <!--<property name="proxyfactory.factory_class">
        NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
        NHibernate.ByteCode.LinFu
    </property>-->
    <property name="proxyfactory.factory_class">
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, 
        NHibernate.ByteCode.Castle
    </property>
</session-factory>

I am just not sure what is missing there... It clearly is talking to the DB cause if I change the name of the database to something that doesn't exist it trows an exception, I am stuck - I have gone round and round on this and just haven't figured it out yet so any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-10-10 12:05:32

请参阅我的评论...不要忘记将您的地图类公开,否则 FluentNHibernate 将看不到它们。

See my comment... Don't forget to make your map classes public or FluentNHibernate won't see them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文