How to configure NHibernate to use connection string from <connectionStrings> configuration section - nhibernate

does anybody know how to configure NHibernate properties file to use a connection string already specified in configuration element?

I found it on google.com:
<connectionStrings>
<add name="connection_string_name" connectionString="[connection string]"/>
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
...
<property name="connection.connection_string_name">connection_string_name</property>
...
</session-factory>
</hibernate-configuration>

Related

How I can set address of WCF service from client's command line when using spring.net

My WCF service can work on any servers. My client - is console application. In command line parameters I want set address of my WCF service.
Current in config client I have:
...
<spring>
<context>
<resource uri="assembly://MyAssembly.Console/MyAssembly.Console/ServerWeb.xml"/>
</context>
</spring>
...
<system.serviceModel>
<client>
<endpoint behaviorConfiguration="Default" name="serverWebDataServiceEndpoint" address="http://localhost/mydata/DataService.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="MyData.Contracts.IDataService"/>
</client>
...
File ServerWeb.xml is:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:wcf="http://www.springframework.net/wcf">
<wcf:channelFactory id="serverWebDataService"
channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
endpointConfigurationName="serverWebDataServiceEndpoint" />
</objects>
In application, I use next code, for call service's methods:
IApplicationContext _ctx = ContextRegistry.GetContext();
IDataService _dataService = _ctx["serverWebDataService"] as IDataService;
var rule = _dataService.GetRuleById(ruleId);
How I can use another address of WCF service from command line?
Try something like that :
<wcf:channelFactory id="serverWebDataService"
channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
endpointConfigurationName="serverWebDataServiceEndpoint">
<!-- You can use classic DI to configure the ChannelFactory<T> instance -->
<wcf:property name="Endpoint.Address">
<object type="System.ServiceModel.EndpointAddress, System.ServiceModel">
<constructor-arg name="uri" value"${serviceUrl}"/>
</object>
</wcf:property>
</wcf:channelFactory>
You can use IVariableSource abstraction to get a property value from commandline. See :
http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource
<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
<property name="VariableSources">
<list>
<object type="Spring.Objects.Factory.Config.CommandLineArgsVariableSource, Spring.Core">
<property name="ArgumentPrefix" value="--" />
<property name="ValueSeparator" value="="/>
</object>
</list>
</property>
</object>
Set the variable in command line like this :
program.exe --serviceUrl=http://localhost/Service.svc

Config NHibernate

I use config file for NHibernate .
I want define more than one session-factrory in same config file .
I do it like this :
`
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="test1">
all properties
</session-factory>
<session-factory name="test2">
all properties
</session-factory>'
The application throw exception :
nhibernate.cfg.HibernateConfigException : An exception occurred parsing configuration : The element 'hibernate-configuration' has invalid child element 'session-factory'
You can't define two session factories in a single config file (the schema does not allow it, and NHibernate doesn't provide a way to access them anyway)
Use separate files or, better yet, one of the code-based approaches, which are more flexible.
See http://fabiomaulo.blogspot.com/2009/07/nhibernate-fluent-configuration.html and http://fabiomaulo.blogspot.com/2009/07/nhibernate-configuration-through.html

Configuring Fluent NHibernate from NHibernate config section

I'm trying to use Fluent NHibernate in my solution by configuring it with the following NHibernate xml configuration section
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="mitre">
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">Data Source=YOUR_DB_SERVER;Database=Northwind;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">TRATE</property>
<!-- HBM Mapping Files -->
<mapping assembly="Markel.Mint.Mitre.Data" />
</session-factory>
</hibernate-configuration>
In my code file, to instantiate ISession:
NH_Cfg.Configuration cfg = new NH_Cfg.Configuration();
cfg.Configure();
Fluently.Configure(cfg).Mappings(m => m.FluentMappings = ????)
My question is that if I have already specified the assembly in the NHibernate config section, do I need to explicitly set FluentMappings? If so, then is it possible to retrieve this data from NHibernate config programmatically?
Thanks
Oz
The mapping assembly in hibernate.cfg.xml is searched for embedded *.hbm.xml files. NHibernate does not know anything about fluent mappings (e.g. ClassMap) as those are introduced by Fluent NHibernate. So you need:
Fluently.Configure(cfg).Mappings(m => m.FluentMappings.AddFromAssemblyOf<SomeDomainType>();
in order to configure NHibernate using your ClassMap mappings.
Thanks for the quick response, James.
Could I do the following then?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="mitre">
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">Data Source=YOUR_DB_SERVER;Database=Northwind;User ID=YOUR_USERNAME;Password=YOUR_PASSWORD;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="default_schema">TRATE</property>
<property name="fluent.nhibernate.fluentmapping">Markel.Mint.Mitre.Core.Domain</property>
</session-factory>
</hibernate-configuration>
Then my code could refer to the property thus:
NH_Cfg.Configuration cfg = new NH_Cfg.Configuration(); cfg.Configure();
Fluently.Configure(cfg).Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.Load(cfg.Properties["fluent.nhibernate.fluentmapping"])));

Set the property hibernate.dialect error message

I am having the following error when configuring mvc3 and Nhibernate. Can anyone guide me what I have missed please.
the dialect was not set. Set the property hibernate.dialect.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: NHibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
Source Error:
Line 16: {
Line 17: NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();
Line 18: configuration.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly());
Line 19: sessionFactory = configuration.BuildSessionFactory();
Line 20: }
My web.config is as follows:
<configSections>
<section name="cachingConfiguration"type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings,Microsoft.Practices.EnterpriseLibrary.Caching"/>
<section name="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
<section name="hibernate-configuration"type="NHibernate.Cfg.ConfigurationSectionHandler,
NHibernate"/>
<appSettings>
<add key="BusinessObjectAssemblies" value="Keeper.API"></add>
<add key="ConnectionString" value="Server=localhost\SQLSERVER2005;Database=KeeperDev;User=test;Pwd=test;"></add>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=localhost\SQLServer2005;Database=KeeperDev;User=test;Pwd=test;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
</session-factory>
</hibernate-configuration>
From your connection string, it appears that you are connecting to a 2k5 server. If so, according to the NHibernate docs, the dialect should be set to NHibernate.Dialect.MsSql2005Dialect.
This is the sample configuration from the docs:
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=(local);Initial Catalog=dbname;User Id=user;Password=********</property>
</session-factory>
You could try if setting dialect to NHibernate.Dialect.MsSql2005Dialect works for you.
I have done a simple and stupid mistake. I have not included the right version of NHibernate.
Thanks #Marjin for replying.

NHibernate and sqlite - Could not compile the mapping document

I'm trying to run an NHibernate over sqlite.
i have two projects:
1. Orange.Database - holds the pocos and daos and everything else
2. Orange.GUI - holds the gui...
when the program reach to the reach the:
Configuration config = new Configuration();
config.AddAssembly("Orange.Database");
sessionFactory = config.Configure().BuildSessionFactory();
an exception is thrown: "Could not compile the mapping document: Orange.Database.Pocos.City.hbm.xml "
inner exception: "Could not find the dialect in the configuration"
city.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Orange.Database.Pocos">
<class name="City" table="Cities">
<id name="Id">
<column name="Id" sql-type="int" not-null="true"/>
<generator class="identity"/>
</id>
<property name="Name">
<column name="name" not-null="true"/>
</property>
<property name="IsTaxFree">
<column name="is_tax_free" not-null="true"/>
</property>
</class>
</hibernate-mapping>
I tried writting the assembly, and then removed it..
the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">
Data Source=C:\Users\Nadav\Documents\Visual Studio 2005\Projects\orange\DB\OrangeDB\OrangeDB.db;Version=3
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
</session-factory>
</hibernate-configuration>
</configuration>
I've tried different location of the db file..
i have tried to remove the configSections
and some other ideas i found on the web...
I'm using vs 2005
NHibernate version is 2.0.1.4000
Any suggestions?
Do this in your code:
Configuration config = new Configuration();
config.Configure();
config.AddAssembly("Orange.Database");
sessionFactory = config.BuildSessionFactory();
First, download the latest stable version of NHibernate, 2.1.2.
Second, try creating a configuration file named hibernate.cfg.xml, containing:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">
Data Source=C:\Users\Nadav\Documents\Visual Studio 2005\Projects\orange\DB\OrangeDB\OrangeDB.db;Version=3
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
</session-factory>
</hibernate-configuration>
I got the same "could not compile error" but I'm not using sqlite.
I got it because I was calling the configure method from the nhibernate configuration instance twice.