tch*_*kch 11 .net c# nhibernate fluent-nhibernate c#-4.0
我有实体模型,我希望每次运行应用程序时都反映到数据库但没有清除数据,因此我使用SchemaUdpate流畅的nhibernate映射方法
var config = Fluently.Configure().Database
(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString));
//here I add mappings , apply conventions, build configuration, etc...
//
new SchemaUpdate(configuBuild).Execute(doUpdate: true, script: true);
Run Code Online (Sandbox Code Playgroud)
所以我的实体模型几乎一直都在正确更新.问题是,当我改变一个属性的定义时,让我说我有这样的属性
[CustomSqlType("nvarchar(400)")]
public virtual string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
CustomSqlType只是在加载映射时将由特定约定应用的属性.在这种情况下,Name属性将创建为nvarchar(400)字段.但如果将来我将定义更改为此
[CustomSqlType("nvarchar(500)")]
public virtual string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
将生成正确的hbm.xml文件(正确表示nvarchar(500))但数据库中的列不是更新事件,尽管此类alter在db透视图中有效.是否可以使用新的长度/精度/可空约束来更改(生成更改脚本)现有列SchemaUpdate?
tch*_*kch 11
好吧,我发现这是不可能的,下面是执行的代码 SchemaUpdate
foreach (Column column in ColumnIterator)
{
IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);
if (columnInfo != null)
{
continue;
}
// the column doesnt exist at all.
// other not important code
}
Run Code Online (Sandbox Code Playgroud)
如您所见,如果列存在,它默认不执行任何操作.