本文整理汇总了C#中NHibernate.Cfg.Configuration.Configure方法的典型用法代码示例。如果您正苦于以下问题:C# NHibernate.Cfg.Configuration.Configure方法的具体用法?C# NHibernate.Cfg.Configuration.Configure怎么用?C# NHibernate.Cfg.Configuration.Configure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NHibernate.Cfg.Configuration
的用法示例。
在下文中一共展示了NHibernate.Cfg.Configuration.Configure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildSessionFactory
public static IDictionary<string, ISessionFactory> BuildSessionFactory()
{
// var configure = new Configuration();
var mapping = GetMappings();
//configure.Configure();
//configure.AddDeserializedMapping(mapping, "NHSchema");
if (!CommonHelper.AllowMultiTenancy(out _ConnectionStringName))
{
var config = new Configuration();
// config.SetInterceptor(new NHSQLInterceptor());
config.Configure();
var _host = EngineContext.Current.Resolve<HttpContextBase>().Request.Url.Authority;
config.AddDeserializedMapping(mapping, "NHSchema");
config.Configure()
.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, _ConnectionStringName)
.
SetProperty(NHibernate.Cfg.Environment.ShowSql, "true")
.SetProperty(NHibernate.Cfg.Environment.BatchSize, "0")
.SetProperty(NHibernate.Cfg.Environment.CacheRegionPrefix, _host);
if (!_allFactories.ContainsKey(_host))
_allFactories.Add(_host, config.BuildSessionFactory());
}
else
{
XmlNodeList nodes = ConfigurationFile.GetElementsByTagName("Tenant");
foreach (XmlNode n in nodes)
{
var config = new Configuration();
// config.SetInterceptor(new NHSQLInterceptor());
config.Configure();
config.AddDeserializedMapping(mapping, "NHSchema");
config.Configure()
.SetProperty(NHibernate.Cfg.Environment.ConnectionString, n["ConnectionString"].InnerText)
.
SetProperty(NHibernate.Cfg.Environment.ShowSql, "true")
.SetProperty(NHibernate.Cfg.Environment.BatchSize, "0")
.SetProperty(NHibernate.Cfg.Environment.CacheRegionPrefix, n["HostName"].InnerText);
// config.SetInterceptor(new NHSQLInterceptor());
if (!_allFactories.ContainsKey(n["HostName"].InnerText))
_allFactories.Add(n["HostName"].InnerText, config.BuildSessionFactory());
}
}
return _allFactories;
}
示例2: Main
static void Main(string[] args)
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
NHibernate.Tool.hbm2ddl.SchemaExport schema = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
schema.Create(false, true);
}
示例3: CreateStatelessDbSession
/// <summary>
/// 创建StatelessDbSession对象
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="dbDialectProvider">数据库特性对象提供程序</param>
/// <param name="mappingXml">实体关系映射配置Xml文本</param>
/// <returns>返回StatelessDbSession对象</returns>
public virtual StatelessDbSession CreateStatelessDbSession(string connectionString, IDbDialectProvider dbDialectProvider, string mappingXml)
{
ISessionFactory sf = null;
while (!sessionFactories.TryGetValue(connectionString, out sf))
{
IDictionary<string, string> dbSetting = new Dictionary<string, string>
{
["dialect"] = dbDialectProvider.DialectName,
["connection.connection_string"] = connectionString,
};
CustomizeNHSessionFactory(dbSetting);
var x = new NHibernate.Cfg.Configuration();
x = x.AddProperties(dbSetting);
//允许采用配置文件修改NHibernate配置
var hc = ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName) as NHibernate.Cfg.IHibernateConfiguration;
if ((hc != null && hc.SessionFactory != null) || File.Exists(GetDefaultConfigurationFilePath()))
{
x = x.Configure();
}
if (System.Transactions.Transaction.Current != null)
{
//如果在分布式事务范围内,就将连接释放模式更改为on_close模式,防止auto模式下,重新获取连接,导致分布式事务升级
x.AddProperties(new Dictionary<string, string> {["connection.release_mode"] = "on_close" });
}
//添加实体关系映射
if (!string.IsNullOrWhiteSpace(mappingXml))
{
x.AddXml(mappingXml);
}
sf = x.BuildSessionFactory();
sessionFactories.TryAdd(connectionString, sf);
}
return new StatelessDbSession(sf, connectionString);
}
示例4: WorkWithOutSharedEngine
public void WorkWithOutSharedEngine()
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
if (TestConfigurationHelper.hibernateConfigFile != null)
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
string[] mappings =
new string[]
{
"Integration.Address.hbm.xml",
"Integration.Tv.hbm.xml",
"Integration.TvOwner.hbm.xml",
"Integration.Martian.hbm.xml",
"Integration.Music.hbm.xml"
};
foreach (string file in mappings)
{
cfg.AddResource("NHibernate.Validator.Tests" + "." + file, Assembly.GetExecutingAssembly());
}
Environment.SharedEngineProvider = null;
XmlConfiguration nhvc = new XmlConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = "true";
nhvc.Properties[Environment.AutoregisterListeners] = "true";
nhvc.Properties[Environment.ValidatorMode] = "UseAttribute";
ValidatorInitializer.Initialize(cfg);
}
示例5: ApplyWrongConstraint
public void ApplyWrongConstraint()
{
XmlConfigurator.Configure();
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
if (TestConfigurationHelper.hibernateConfigFile != null)
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
cfg.AddResource("NHibernate.Validator.Tests.Integration.WrongClass.whbm.xml", Assembly.GetExecutingAssembly());
Environment.SharedEngineProvider = null;
XmlConfiguration nhvc = new XmlConfiguration();
nhvc.Properties[Environment.ApplyToDDL] = "true";
nhvc.Properties[Environment.AutoregisterListeners] = "true";
nhvc.Properties[Environment.ValidatorMode] = "UseAttribute";
using (LoggerSpy ls = new LoggerSpy(typeof(ValidatorInitializer), Level.Warn))
{
ValidatorInitializer.Initialize(cfg);
int found =
ls.GetOccurenceContaining(
string.Format("Unable to apply constraints on DDL for [MappedClass={0}]", typeof (WrongClass).FullName));
Assert.AreEqual(1, found);
found =
ls.GetOccurenceContaining(
string.Format("Unable to apply constraints on DDL for [MappedClass={0}]", typeof (WrongClass1).FullName));
Assert.AreEqual(1, found);
}
}
示例6: NhibernateConfigurator
protected NhibernateConfigurator(bool mapDtoAssembly)
{
var assembliesToMap = GetAssembliesToMap(mapDtoAssembly);
var includeBaseTypes = GetIncludeBaseTypes();
var ignoreBaseTypes = GetIgnoreBaseTypes();
var discriminatedTypes = GetDiscriminatedTypes();
var mapDefaultConventions = ShouldMapDefaultConventions();
var assemblyWithAdditionalConventions = GetAssembliesWithAdditionalConventions();
_configuration = new Configuration();
_configuration.Configure();
var autoPersistenceModel = AutoMap.Assemblies(new AutomappingConfiguration(discriminatedTypes.ToArray()), assembliesToMap);
includeBaseTypes.Each(x => autoPersistenceModel.IncludeBase(x));
ignoreBaseTypes.Each(x => autoPersistenceModel.IgnoreBase(x));
assembliesToMap.Each(x => autoPersistenceModel.UseOverridesFromAssembly(x));
if (mapDefaultConventions) autoPersistenceModel.Conventions.AddFromAssemblyOf<PrimaryKeyConvention>();
assemblyWithAdditionalConventions.Each(x => autoPersistenceModel.Conventions.AddAssembly(x));
_sessionFactory = Fluently.Configure(_configuration)
.Mappings(x =>
{
var mappingsContainer = x.AutoMappings.Add(autoPersistenceModel);
var exportNhibernateMappingsFolder = ConfigurationManager.AppSettings["ExportNhibernateMappingsFolder"];
if (!string.IsNullOrWhiteSpace(exportNhibernateMappingsFolder)) mappingsContainer.ExportTo(exportNhibernateMappingsFolder);
})
.BuildSessionFactory();
}
示例7: Application_Start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
var configuration = new NHibernate.Cfg.Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(doCS.Models.Project).Assembly);
SessionFactory = configuration.BuildSessionFactory();
// Build up your application container and register your dependencies.
var builder = new ContainerBuilder();
builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());
builder.Register(x => SessionFactory.OpenSession()).As<ISession>().HttpRequestScoped();
builder.RegisterType<doCS.Web.Helpers.Implimentation.ProjectContext>().As<doCS.Web.Helpers.IProjectContext>().HttpRequestScoped();
//register extractor helpers
builder.RegisterType<doCS.Web.Helpers.Implimentation.Extractor.ExtractorHelper>().As<doCS.Web.Helpers.IExtractorHelper>().HttpRequestScoped();
builder.RegisterType<doCS.Web.Helpers.Implimentation.Extractor.ProjectUpdaterProvider>().InstancePerDependency();
//register extractor dependencies
builder.RegisterType<doCS.Extractor.Implementation.Extractor>().As<doCS.Extractor.IExtractor>().HttpRequestScoped();
builder.RegisterType<doCS.Extractor.Implementation.DllExtractor>();
builder.RegisterType<doCS.Extractor.Implementation.XmlExtractor>();
//register view helpers
builder.RegisterType<doCS.Web.ViewHelpers.XmlDocumentationHelper>();
_containerProvider = new ContainerProvider(builder.Build());
ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));
SetupAutoMapper();
}
示例8: GetSessionFactoryFor
/// <summary>
/// This method attempts to find a session factory stored in <see cref="_sessionFactories" />
/// via its name; if it can't be found it creates a new one and adds it the hashtable.
/// </summary>
/// <param name="sessionFactoryConfigPath">Path location of the factory config</param>
public ISessionFactory GetSessionFactoryFor(string sessionFactoryConfigPath) {
Check.Require(!string.IsNullOrEmpty(sessionFactoryConfigPath),
"sessionFactoryConfigPath may not be null nor empty");
// Attempt to retrieve a stored SessionFactory from the hashtable.
ISessionFactory sessionFactory = (ISessionFactory) _sessionFactories[sessionFactoryConfigPath];
// Failed to find a matching SessionFactory so make a new one.
if (sessionFactory == null) {
Check.Require(File.Exists(sessionFactoryConfigPath),
"The config file at '" + sessionFactoryConfigPath + "' could not be found");
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(sessionFactoryConfigPath);
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null) {
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
_sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
}
return sessionFactory;
}
示例9: Main
public static void Main(string[] args)
{
IList<Product> products;
// Don't need to use schema export because of the hbm2dll property.
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
// ensure that mapping hbm.xml file is loaded
cfg.AddAssembly(typeof(MainClass).Assembly);
Product p = new Product() {Name="Captains of Crush Gripper #1", Category="fitness" };
ISessionFactory factory =
cfg.BuildSessionFactory();
using (ISession session = factory.OpenSession())
{
session.Save(p);
session.Flush();
ICriteria sc = session.CreateCriteria<Product>();
products = sc.List<Product>();
Console.WriteLine(products[0].Name);
session.Close();
}
factory.Close();
Console.WriteLine( products.Count );
Console.WriteLine ("Hello World!");
}
示例10: GetDefaultConfiguration
/// <summary>
/// Standar Configuration for tests.
/// </summary>
/// <returns>The configuration using merge between App.Config and hibernate.cfg.xml if present.</returns>
public static NHibernate.Cfg.Configuration GetDefaultConfiguration()
{
NHibernate.Cfg.Configuration result = new NHibernate.Cfg.Configuration();
if (hibernateConfigFile != null)
result.Configure(hibernateConfigFile);
return result;
}
示例11: ConfigureNHibernate
public NHibernate.Cfg.Configuration ConfigureNHibernate()
{
var cfg = new NHibernate.Cfg.Configuration();
if (TestConfigurationHelper.hibernateConfigFile != null)
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
cfg.AddResource("NHibernate.Validator.Tests.Specifics.NHV82.Person.hbm.xml", Assembly.GetExecutingAssembly());
return cfg;
}
示例12: SessionManager
private SessionManager()
{
Configuration config = new Configuration();
config.Properties[NHibernate.Cfg.Environment.ConnectionString] =
ConfigurationManager.ConnectionStrings["AtomicCms"].ConnectionString;
config.Configure();
this.sessionFactory = config.BuildSessionFactory();
}
示例13: CreateDatabase
/// <summary>
/// Generate the database schema, apply it to the database and save the DDL used into a file.
/// </summary>
public static void CreateDatabase()
{
Configuration configuration = new Configuration();
configuration.Configure();
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.SetOutputFile("SQLite database schema.ddl");
schemaExport.Create(true, true);
}
示例14: GetSessionFactory
public static ISessionFactory GetSessionFactory()
{
var configuration = new NHibernate.Cfg.Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(SessionFactoryFactory).Assembly.GetName().Name);
log4net.Config.XmlConfigurator.Configure();
var sessionFactory = configuration.BuildSessionFactory();
return sessionFactory;
}
示例15: InitializeSessionFactory
private static ISessionFactory InitializeSessionFactory()
{
var cfg = new NHibernate.Cfg.Configuration();
cfg.Configure();
return Fluently.Configure(cfg)
.Mappings(m => m.AutoMappings
.Add(CreateAutomappings))
.BuildSessionFactory();
}