5
votes

I need help with a problem in my application.

I've created a project in Visual Studio, and I've installed and configured Fluent NHibernate. I was able to configure everything correctly, and I was able to connect to my database.

I tried to insert a few rows into my table, and everything was okay, but whenever I restarted my application, I noticed that the tables were being dropped and re-created, and all the data were gone.

I solved this problem by using:

.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))

instead of:

.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))

Everything so far is okay, but now here's the problem. Let's say I have a class Person.cs with two fields: Id, Name. And let's say I wanted to add a new property called Address. I add the property to my class and run my application. I try to add a few rows and check the database to see that indeed, a new column called Address was created and null values were inserted for old entries.

Now let's say I changed my mind and decided to remove the new property Address. I remove the property from my Person.cs class and run the application. I insert a few entries and check the database to see that the column Address was not removed and is still there in the table.

Why is this happening exactly? Shouldn't the mapping configuration do everything for me? I expect the no-longer needed columns to be dropped in the database the same way they are created when I add new properties to my class and without losing my data. What should I do?

1

1 Answers

4
votes

SchemaExport/SchemaUpdate are not a migrations framework.

They only make sure your domain can be used with the database, and are useful during development (not in production)