【问题标题】:NHibernate SchemaExport does not create ntext columnsNHibernate SchemaExport 不创建 ntext 列
【发布时间】:2015-09-21 11:24:08
【问题描述】:

我创建了一个简单的工具,它使用SchemaExport 来生成数据库和 sql 脚本。在一个简单的实体上,一个字符串属性 Description 在 SQL Server 中应该是 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;
    }
}
}

【问题讨论】:

  • 尝试其他长度?像 10562 一样,超过 8000 的值(varchar 的 SQL 服务器限制)位与 MaxValue 不同,可能会被视为特殊情况值?
  • @SimonSvensson 感谢您的建议。如果
  • ntext、text 和 image 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)、varchar(max) 和varbinary(max)。 See details here
  • @marc_s 是的,我现在明白了。谢谢。

标签: c# nhibernate orm nhibernate-mapping schemaexport


【解决方案1】:

经过进一步阅读,ntext 将在未来的版本中与 text and image https://msdn.microsoft.com/en-us/library/ms187993.aspx 一起被删除

ntext 、 text 和 image 数据类型将在 Microsoft SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用 nvarchar(max)、varchar(max) 和 varbinary(max)。

所以这段代码可以工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2011-03-28
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多