SchemaExport的create/drop、nvarchar的长度总是1

本文关键字:create drop SchemaExport nvarchar | 更新日期: 2023-09-27 18:12:00

我想在测试中创建/删除模式:

[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>

SchemaExport的create/drop、nvarchar的长度总是1

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

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

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

或者你可以明确地指定sql-type但是在你的例子中你不能使用长度但是你需要写出完整的类型nvarchar(256):

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