NHibernate SchemaExport 不创建 ntext 列

NHibernate SchemaExport does not create ntext columns

我创建了一个简单的工具,它使用 SchemaExport 生成数据库和 sql 脚本。在一个简单的实体上,一个字符串 属性 Description 应该是 SQL 服务器中的 ntext 列,但实际上它是 nvarchar(255).

不知道我哪里错了,欢迎大家指教!

下面是我的代码,只需创建一个控制台应用程序 + 添加 NHibernate nuget 包到 运行。

using System;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Tool.hbm2ddl;

namespace ConsoleApplication1
{
public class Item
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class ItemMap : ClassMapping<Item>
{
    public ItemMap()
    {
        Id(e => e.Id, m => m.Generator(Generators.Identity));

        Property(e => e.Description, m =>
        {
            m.NotNullable(true);
            m.Length(int.MaxValue);
        });
    }
}

class Program
{
    private const string ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=db01;Integrated Security=True";

    static void Main(string[] args)
    {
        var modelMapper = BuildModelMapper();
        var configuration = GetConfiguration();
        configuration.AddDeserializedMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities(), null);

        try
        {
            new SchemaExport(configuration).Execute(false, true, false);
            Console.WriteLine("Done");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.ReadLine();
    }

    private static ModelMapper BuildModelMapper()
    {
        var mm = new ModelMapper();
        mm.AddMapping(typeof(ItemMap));
        return mm;
    }

    private static Configuration GetConfiguration()
    {
        var cfg = new Configuration();

        cfg.DataBaseIntegration(db =>
        {
            db.Driver<SqlClientDriver>();
            db.Dialect<MsSql2008Dialect>();
            db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
            db.ConnectionString = ConnectionString;
            db.LogFormattedSql = true;
            db.LogSqlInConsole = true;
            db.AutoCommentSql = true;
        });

        return cfg;
    }
}
}

进一步阅读后,ntext 将在以后的版本中与 text and image https://msdn.microsoft.com/en-us/library/ms187993.aspx

一起删除

ntext , text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

所以这段代码可以工作

Property(e => e.Description, m =>
    {
        m.NotNullable(true);
        m.Length(4001); // any value > 4K
    });