【问题标题】:NHibernate automatically rename the column name in C#NHibernate 自动重命名 C# 中的列名
【发布时间】:2008-10-25 01:41:45
【问题描述】:

我不明白 NHibernate 为什么要这样做。

这是我在 MS SQL 中的表的一部分

CREATE TABLE Person ( 
nameFamily text,
nameGiven text
)

这里是 Person.hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <class name="Infosci.Dpd.Model.Person,Infosci.Dpd.Model" table="Person" lazy="true">
    <property type="string"  name="NameGiven" column="nameGiven" />
    <property type="string" name="NameFamily" column="nameFamily" />
  </class>
</hibernate>

在我的 Person.cs 中,我将我的属性命名如下

    string NameFamily
    {
        get ;
        set ;

    }
    string NameGiven
    {
        get ;
        set ;

    }

然后我尝试使用以下代码创建记录

    ISession session = NHibernateHelper.GetCurrentSession();
    Person person = new Person();
    person.NameFamily = "lee";
    session.Save(person);

奇怪的是,当我尝试执行它时,NHibernate 实际上通过执行以下操作来更改列名

NHibernate: INSERT INTO Person (name_family,name_given.......

因此,NHibernate 在这里将我的列名从 NameFamily 更改为 name_family,尽管我确实指定了列名。

这是一个 NHibernate 错误吗?或者有什么我需要做的配置吗?

这是我的休眠配置文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

  <session-factory>

    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=xxxxxxx;Initial Catalog=xx;Integrated Security=True;Pooling=False;User ID=xxxx;Password=xxxx</property>
    <property name="show_sql">true</property>

  </session-factory>

</hibernate-configuration>

这就是我创建 sessionFactory 的方式

            sessionFactory = new `Configuration().Configure("hibernate.cfg.xml").
SetNamingStrategy(ImprovedNamingStrategy.Instance).
AddFile("Person.hbm.xml").
BuildSessionFactory();`

【问题讨论】:

    标签: c# nhibernate hibernate


    【解决方案1】:

    这是 SetNamingStrategy(ImprovedNamingStrategy.Instance) 的“功能”

    它会在混合大小写名称中的每个大写字母前添加下划线。

    不确定你是如何禁用它的(我不是 nhibernate 用户)

    【讨论】:

      【解决方案2】:

      我最终做的只是使用默认命名策略或 SetNamingStrategy(DefaultNamingStrategy.Instance)

      默认命名策略将保持混合大小写名称不变

      【讨论】:

      • 或者你可以不指定命名策略而不是指定默认策略。
      猜你喜欢
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2016-06-06
      相关资源
      最近更新 更多