【发布时间】: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