2

I create a simple tool which uses SchemaExport to generate database & sql scripts. On a simple entity, one string property Description is expected a ntext column in SQL Server but infact it is nvarchar(255).

enter image description here

Do not sure that which part I was wrong, any advice is appreciated!

Below is my code, just create a console app + add NHibernate nuget package to run.

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;
    }
}
}
hazjack
  • 1,645
  • 13
  • 27
  • Tried another length? Like 10562, a value over 8000 (SQL server limit for varchar) bit different from MaxValue which may may be treated like a special case value? – sisve Jul 03 '15 at 15:15
  • @SimonSvensson thanks for your suggestion. If <=4K, it generates nvarchar(x), otherwise nvarchar(max). – hazjack Jul 03 '15 at 15:20
  • 2
    `ntext`, `text`, and `image` data types will be removed in a future version of 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. [See details here](http://msdn.microsoft.com/en-us/library/ms187993.aspx) – marc_s Jul 03 '15 at 15:41
  • @marc_s yes, I see now. Thank you. – hazjack Jul 03 '15 at 15:46

1 Answers1

3

After some further reading, the ntext will be removed in future versions along with 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.

So this code will work

Property(e => e.Description, m =>
    {
        m.NotNullable(true);
        m.Length(4001); // any value > 4K
    });
hazjack
  • 1,645
  • 13
  • 27