Use variable table name in NHibernate mapping document

316 views
Skip to first unread message

Tys

unread,
Jun 6, 2011, 7:47:41 AM6/6/11
to nhusers
Hi Everyone,

I'm creating a small application that connects to a SQL database and
passes some values to another application.
The problem is that the name of the SQL table that i have to map, can
change, so therefore i'm looking for a way to use a variable in this
part of the mapping document:

My mapping file looks like this
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-
import="true" namespace="TheSyncTool">
<class name="SalesHeader, TheSyncTool" table="[CompanyA$Sales
Header]" lazy="false" dynamic-update="true">

The prefix of the tablename "CompanyA" is what i want to be
configurable. Is there a way to do such a thing?
Otherwise i have to compile the project with a modified mapping file
everytime a client creates a new company in the database.

Thanks for your help!
Best regards, Tys

sbohlen

unread,
Jun 6, 2011, 1:35:17 PM6/6/11
to nhu...@googlegroups.com
Tys:

There is no requirement that any (and certainly not all) mapping files be compiled into your assembly as embedded resources.  You can easily mix and match embedded and stand-alone XML mapping files in your application.

In your situation, I would embed the mapping file(s) not expected to change into an assembly as resources but leave the mapping file(s) that are expected to change as externalized file(s) that can be edited as needed after the application is deployed.  Since its all just XML, you can edit it in any text editor, provide a custom application to your customer to enable them to specify the name of the table they want in the mapping file, etc.

Hope this helps.

-Steve B.

Tys

unread,
Jun 6, 2011, 7:15:36 PM6/6/11
to nhusers
Hi Steve,
Thanks for your answer. It sounds hopefull! I didn't know i could
leave mappings out of my assembly. Because, always when i forget to
set it as an embedded resource, i'm getting errors.
Can you give me a hint on how to work with / where to place / a
mapping file when i don't want embed it?
Thanks for your help!
Tys

Fabio Maulo

unread,
Jun 8, 2011, 7:22:12 AM6/8/11
to nhu...@googlegroups.com
It is a file, you can put it where you want.
You can add it through the hibernate.cfg.xml (section <mapping>); using the xsd to write the hibernate.cfg.xml you can see this help:
There are 3 possible combinations of mapping attributes
1 - resource & assembly:  NHibernate will read the mapping resource from the specified assembly
2 - file only: NHibernate will read the mapping from the file.
3 - assembly only: NHibernate will find all the resources ending in hbm.xml from the assembly.

or through the Configuration. methods:
AddFile(FileInfo xmlFile)
AddXmlFile(string xmlFile)
AddUrl(string url)
AddUrl(Uri url)

Tys

unread,
Jun 9, 2011, 6:03:29 PM6/9/11
to nhusers
Thanks Fabio,
I'm going to try some of the options tomorrow!
Reply all
Reply to author
Forward
0 new messages