11

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 ?

tchrikch
  • 2,428
  • 1
  • 23
  • 43

1 Answers1

12

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.

tchrikch
  • 2,428
  • 1
  • 23
  • 43
  • That really stinks. What did you end up doing to force an update on your schema? – Gaute Løken Nov 15 '11 at 13:24
  • thanks! I've spent ages trying to find out why it doesn't seem to update my default constraints! - Did you manage to find a workaround? – Zack Feb 03 '12 at 18:41