Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter column using nhibernate SchemaUpdate functionality

I have entity model which I want to be reflected to database each time I run application but without clearing the data thus I'm using SchemaUdpate with fluent nhibernate mappings method in a way

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);

So my entity model gets updated correctly almost all the time. The problem is that when I alter definition of a property, lets say I had such property

 [CustomSqlType("nvarchar(400)")]
 public virtual string Name { get; set; }

CustomSqlType is just an attribute that will be applied by a certain convention when mappings are loaded. In this case, Name property would be created as nvarchar(400) field. But if in the future I change the definition to this

 [CustomSqlType("nvarchar(500)")]
 public virtual string Name { get; set; }

than correct hbm.xml file would be generated (correct means nvarchar(500) ) but the column in the database is not updated event though such alter is valid from db perspective. Is it possible to alter(generate alter script) existing column with new length/precision/nullable constraint using SchemaUpdate ?

like image 686
tchrikch Avatar asked Nov 02 '11 13:11

tchrikch


1 Answers

Okay I found that it's impossible , below there is code executed by 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
        }

As you see it does nothing by default if the column exists.

like image 50
tchrikch Avatar answered Oct 11 '22 13:10

tchrikch