NHibernate
Contents
Konfigurationsdatei Sample
sqlserver sample
<?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="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=(local);Database=nhibernate;Trusted_Connection=true;</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Mysql example
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<appSettings>
<add key="loglevel" value="info"/>
<add key="logimplementation" value="NLOG"/>
<add key="exportdirectory" value="/tmp" />
</appSettings>
<connectionStrings>
<add name="testmasterconnection"
connectionString="server=localhost;user=root;pwd=dukannstmichmal;database=mydb;port=3306;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<property name="connection.connection_string_name">testmasterconnection</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.keywords">none</property>
</session-factory>
</hibernate-configuration>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="out.txt" />
<target name="console" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minLevel="Info" writeTo="logfile" />
<logger name="*" minLevel="Info" writeTo="console" />
<logger name="TagsManagement" minLevel="Debug" writeTo="console" />
</rules>
</nlog>
</configuration>
many to many mapping
Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ConsoleApplication2"
namespace="ConsoleApplication2">
<class name="MyItem">
<id name="ID" generator="native"/>
<property name="Name" />
<!-- Many-to-many mapping: OrderItems -->
<bag name="Tags"
table="MyItem_MyTag"
cascade="none"
lazy="true">
<key column ="MyItemID" />
<many-to-many class="MyTag" column="MyTagID" />
</bag>
</class>
</hibernate-mapping>
Klasse:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class MyItem
{
public virtual string Name { get; set; }
public virtual int ID { get; set; }
IList<MyTag> _tags = new List<MyTag>();
public virtual IList<MyTag> Tags {
get
{
return this._tags;
}
set
{
_tags = value;
}
}
}
}
Konfiguration mit fluet configuration
//Cache einschalten:
Cache.ReadOnly().Region("Keep15Min");
//Fremdschlüssel ohne Fremdschlüsselconstraint
References(x => x.Verantwortlicher, "strVerantwortlicher").Nullable()
.ForeignKey("FK_Vertrag_Verantwortlicher_Employee")
.Index("IX_strVerantwortlicherId")
.NotFound.Ignore()
.Access.CamelCaseField(Prefix.Underscore);