Reputation: 4595
I'm a little confused about what goes on behind the scenes in NHibernate when I add a new property to a class.
When I add a property 'Price' to an 'Item' class, my application throws an exception when retrieving Item objects because this new property isn't in the database:
[SqlException (0x80131904): Invalid column name 'Price'.]
Am I supposed to manually add a column to my DB table everytime I declare a new property in a class?
Upvotes: 2
Views: 2403
Reputation: 123861
In general, we can use configuration setting for updating our DB schema whenever ISessionFactory
is created:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="hbm2ddl.auto">Update</property>
Check Table 3.2. NHibernate Configuration Properties
We can do it even in our own code:
How to update database table schemas with NHibernate schema generation?
(snippet from Q & A above)
_cfg = new Configuration();
_cfg.Configure();
_cfg.AddAssembly(Assembly.LoadFrom("DataLayer.dll"));
var update = new SchemaUpdate(_cfg);
update.Execute(true, false);
Summary, we can set our Factory to update schema, whenever such Factory is created. It could be used in DEV, but I would say, in DEV only...
Upvotes: 1
Reputation: 1274
The short answer is: yes, you are.
The class that you're asking nHibernate to map to a table in the database must only have properties that exist as columns in that table. If you want a new property in that class then you need to add a column in the table.
On the other hand, if you want a "Price" property for the Item that's not persisted in the database, then you would have a different class in the application, not under the control of nHibernate, which contains an object of class "Item", with its own "Price" property.
Upvotes: 1