SchemaExport创建/删除,nvarchar的长度始终为1

时间:2013-05-04 06:04:11

标签: c# nhibernate

我想在测试中创建/删除架构:

[SetUp]
public void Setup()
{
   var config = new Configuration();

  config.Configure(Path.Combine(Environment.CurrentDirectory, "hibernate.cfg.xml"));
  (new[] { typeof(Entity).Assembly }).ToList().ForEach(a => config.AddAssembly(a));

  var export = new SchemaExport(config);

   export.Create(false, true);
}

[TearDown]
public void TearDown()
{
   var config = new Configuration();

  config.Configure(Path.Combine(Environment.CurrentDirectory, "hibernate.cfg.xml"));
  (new[] { typeof(Entity).Assembly }).ToList().ForEach(a => config.AddAssembly(a));

  var export = new SchemaExport(config);

   export.Drop(false, true);
}

生成表格时,所有nvarchar列的长度均为1.您能解释一下原因吗?

这是映射的示例:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="TSS.Domain" namespace="TSS.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Status" table="DicStatus" schema="dbo" lazy="false">
    <id name="Id" type="Guid">
      <column name="id" not-null="true" sql-type="uniqueidentifier" />
      <generator class="guid.comb" />
    </id>
    <property name="Name" type="String">
      <column name="name" not-null="true" length="256" sql-type="nvarchar" />
    </property>
    <property name="CreatedAt" type="DateTime">
      <column name="createdAt" not-null="true" sql-type="datetime" />
    </property>

  </class>
</hibernate-mapping>

这是配置:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">***;</property>
    <property name="adonet.batch_size">100</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="max_fetch_depth">1</property>
    <property name="command_timeout">60</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
   </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:2)

仅当您使用默认列类型时才使用长度,因此当您没有指定sql-type时。所以,如果你写

<property name="Name" type="String">
   <column name="name" not-null="true" length="256" />
</property>

它将使用name生成nvarchar(256)列,因为nvarchar是MsSql2008Dialect中字符串的默认列类型。

或者您可以明确指定sql-type,但在您的情况下,您不能使用长度,但您需要写出完整类型nvarchar(256)

<property name="Name" type="String">
   <column name="name" not-null="true" sql-type="nvarchar(256)" />
</property>