Sometimes there is a need to add more columns to a table, but the code model can't be updated to accommodate this. Can we still add the columns, persist, and query their values? Yes, NHibernate provides a way to do map table columns into a key-value IDictionary
, instead of individual properties of the class. This is called a dynamic-component.
Add a new folder named
DynamicComponents
to theMappingRecipes
project.Add a new class named
Contact
to the folder:using System; using System.Collections; namespace MappingRecipes.DynamicComponents { public class Contact { public Contact() { Attributes=new Hashtable(); } public virtual Guid Id { get; protected set; } public virtual IDictionary Attributes { get; set; } } }
Add an embedded mapping named
Contact.hbm.xml
to the folder:<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns...