sql server - How to verify if NHibernate hbm.xml are matching with a particular SQL schema? -
i had asp.net mvc web application, using nhibernate orm on sql server 2008 r2. when deployed server, can update our database time (some ad-hoc changes).
the problem when database schema change, application crashed because nhibernate .hbm.xml files no longer matching db schema.
how verify current *.hbm.xml file matching database schema ? , how detect mismatch in asp.net mvc ?
you can checking when application runs, in global asax.
protected void application_start() { }
the connection string key expected schema.
<property name="connection.connection_string">server=.;initial catalog=theexpectedschema; ..</property>
first read expected schema reading nhibernate config , retrieve initial catalog
part (if database oracle, use user id
part).
nhibernate.cfg.configuration config = ...; var constr = config.properties["connection.connection_string"]; var match = regex.match(constr, "initial catalog *= *([^;]*) *"); var expectedschema = match.groups[1].value;
then read actual schema reading *.hbm.xml
file.
<hibernate-mapping schema="theactualschema"
if files put under app_data
directory, read each file , use xml document schema.
var appdatadir = new directoryinfo(httpcontext.server.mappath("~/app_data")); var files = appdatadir.getfiles("*.hbm.xml"); foreach (var file in files) { var doc = new xmldocument(); doc.load(file.fullname); var actualschema = doc.documentelement.getattribute("schema"); if (actualschema != expectedschema) { // proper handling here (an example throwing exception). throw new exception(string.format("expected schema: {0}, actual schema {1}", expectedschema, actualschema)); } }
Comments
Post a Comment