chotkos
chotkos

Reputation: 180

NHibernate SchemaUpdate still wants to recreate table

I am using NHibernate with Fluent on my SQL Server 2012 and I've decided to use SchemaUpdater instead of SchemaExport becouse I dont want my data to disappear. I've created method like this:

 public void UpdateSchema(Configuration config)
    {
        System.Action<string> updateExport = x =>
        {
            using (var file = new System.IO.FileStream(@"C:\tmp\update.sql", System.IO.FileMode.Append, System.IO.FileAccess.Write))
            using (var sw = new System.IO.StreamWriter(file))
            {
                sw.Write(x);
                sw.Close();
            }
        };
        NHibernate.Tool.hbm2ddl.SchemaUpdate SchemaUpdater = new NHibernate.Tool.hbm2ddl.SchemaUpdate(config);
        SchemaUpdater.Execute(updateExport, true); 

    }

My database is creating but when i want to add new field to my model i get an error:"Invalid column name 'xxx'. ↵Invalid column name 'xxx'."

I've decided to debug and check SchemaUpdater.Exceptions, there was:

Count = 4
[0]: {"There is already an object named 'User' in the database."}
[1]: {"There is already an object named 'FK5E008E27F9F9FA61' in the database.\r\nCould not create constraint. See previous errors."}
[2]: {"There is already an object named 'FK5E008E27194964AD' in the database.\r\nCould not create constraint. See previous errors."}
[3]: {"There is already an object named 'FK5E008E27713F97F9' in the database.\r\nCould not create constraint. See previous errors."}

This looks like program wants still to create new table, not to update it, sql which is created and sent to db:

create table [User] (
    Id INT IDENTITY NOT NULL,
   Acount NVARCHAR(255) null,
   Email NVARCHAR(255) null,
   (...)
   xxx NVARCHAR(255) null,
   Manager_id INT null,
   Evaluator_id INT null,
   WorkTimeAdmin_Id INT null,
   primary key (Id)
)
alter table [User] 
    add constraint FK5E008E27F9F9FA61 
    foreign key (Manager_id) 
    references [User]
alter table [User] 
    add constraint FK5E008E27194964AD 
    foreign key (Evaluator_id) 
    references [User]
alter table [User] 
    add constraint FK5E008E27713F97F9 
    foreign key (WorkTimeAdmin_Id) 
    references [User]

I really want an automated solution, so no copying and editing sql's any ideas what should i change?

Upvotes: 0

Views: 1168

Answers (2)

DaEkstrim
DaEkstrim

Reputation: 36

In addition to Ricardo's answer, here's an example of how the hbm2ddl.auto property is set. The "update" instructs the Session Factory to create tables that don't exist and update existing ones if schema and relative model are different. I learnt the hard way that "update" is case sensitive, so "Update" doesn't work.

.ExposeConfiguration(cfg => {
                    cfg.SetProperty("hbm2ddl.auto", "update"); // Updates or creates tables                                                
                })

Upvotes: 1

Ricardo Peres
Ricardo Peres

Reputation: 14555

You never need to run SchemaUpdate explicitly. The Configuration instance can take a property "hbm2ddl.auto" (NHibernate.Cfg.Environment.Hbm2ddlAuto), that, when set to "update" (SchemaAutoAction.Update), will update the database if needed.

Upvotes: 1

Related Questions