使用Nhibernate';s代码映射
我想使用代码映射,因此我有一个类Employee(使用Nhibernate';s代码映射,nhibernate,mapping-by-code,nhibernate-configuration,conform,Nhibernate,Mapping By Code,Nhibernate Configuration,Conform,我想使用代码映射,因此我有一个类Employee(名称空间NHibernateTests.Classes)和一个类EmployeeMappings(名称空间NHibernateTests.Mappings) 我的整个nhibernate配置设置在一个xml文件hibernate.cfg.xml中,该文件当前如下所示: <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibe
名称空间NHibernateTests.Classes
)和一个类EmployeeMappings(名称空间NHibernateTests.Mappings
)
我的整个nhibernate配置设置在一个xml文件hibernate.cfg.xml
中,该文件当前如下所示:
<?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.OracleClientDriver</property>
<property name="connection.connection_string">User Id=NHIBERNATE;Password=NHIBERNATE;Data Source=XE</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<mapping assembly="NHibernateTests"/>
</session-factory>
</hibernate-configuration>
名称空间NHibernateTests.Mappings
{
公共类EmployeeMappings:类映射
{
公共雇员映射()
{
this.Id(e=>e.Id,mapper=>
{
mapper.Generator(Generators.HighLow);
});
}
}
}
使用按代码映射,您还应按代码配置工厂。有几个方法之一:
引用的代码片段(有关更多详细信息,请参见上面的链接)
如何获得HbmMapping
private static HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}
通过代码映射,您还应该通过代码配置工厂。有几个方法之一: 引用的代码片段(有关更多详细信息,请参见上面的链接) 如何获得
HbmMapping
private static HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}
private static Configuration ConfigureNHibernate()
{
var configure = new Configuration();
configure.SessionFactoryName("BuildIt");
configure.DataBaseIntegration(db =>
{
db.Dialect();
db.Driver();
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
db.IsolationLevel = IsolationLevel.ReadCommitted;
db.ConnectionStringName = "NH3";
db.Timeout = 10;
// enabled for testing
db.LogFormattedSql = true;
db.LogSqlInConsole = true;
db.AutoCommentSql = true;
});
var mapping = GetMappings();
configure.AddDeserializedMapping(mapping, "NHSchemaTest");
SchemaMetadataUpdater.QuoteTableAndColumns(configure);
return configure;
}
private static HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}