forked from 218223420/EmployerForm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionFactory.cs
75 lines (70 loc) · 2.48 KB
/
SessionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployerForm
{
public class SessionFactory
{
private static volatile ISessionFactory iSessionFactory;
private static object syncRoot = new Object();
public static ISession OpenSession
{
get
{
if (iSessionFactory == null)
{
lock (syncRoot)
{
if (iSessionFactory == null)
{
iSessionFactory = BuildSessionFactory();
}
}
}
return iSessionFactory.OpenSession();
}
}
private static ISessionFactory BuildSessionFactory()
{
try
{
string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql7
.ConnectionString(x => x.Server("DESKTOP-43EPCI7;MultipleActiveResultSets=True")
.Database("ALNASR_STEEL")
.Username("sa")
.Password("123456")
))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
throw ex;
}
}
//Create Session
private static AutoPersistenceModel CreateMAppings()
{
return AutoMap
.Assembly(System.Reflection.Assembly.GetCallingAssembly())
.Where(testc => testc.Namespace == "EmployerForm.Model");
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
SchemaUpdate su = new SchemaUpdate(config);
su.ExecuteAsync(true, true);
}
}
}