2015年5月25日 星期一

[C] File mappings and domain of NHibernate for SQLite DB

C#中針對SQLite使用NHibernate來存取DB資料

1. 在C#專案裡需安裝的Packages

NHibernate
Iesi.Collections

2. 額外使用到的軟體 (產出cs與xml檔案)
NHibernateMappingGenerator

設定參考

Entity不修改
Namespace (Domain)、Namespace (Map)、Assembly Name都設為專案名
 Connection選擇SQLite,並修改Data Source的路徑指向DB file

最後按下Generate All即可產出cs與xml檔案


3. 在C#專案中建立Domain與Mapping資料夾,分別放入cs檔案與xml檔案

4. 在C#專案中建立hibernate.cfg.xml
 
    NHibernate.Driver.SQLite20Driver
    Data Source=[DB檔案路徑];Version=3
    NHibernate.Dialect.SQLiteDialect
    true=1;false=0
    true
   
 

5. 在C#專案中新增一class 'NHibernateHelper'

namespace [專案名]
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
完成以上設置即可開始實作相關DB存取程式

2015年5月15日 星期五

[C#] Auto load x86/x64 SQLite DLL for Platform

  How to Reference Platform Specific (X86/x64) Assemblies in Visual Studio Projects


Unload對想要有此功能之Project,Edit xxx.csproj,新增下方內容


<!--Start Code-->
<choose></choose>
<when condition="$(Platform) == 'x64'"></when>
<itemgroup></itemgroup><br />
<reference include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64"></reference>
<specificversion>False</specificversion>
<hintpath>..SQLiteX64System.Data.SQLite.DLL</hintpath>
<otherwise></otherwise>
<itemgroup></itemgroup>
<reference include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86"></reference>
<specificversion>False</specificversion>
<hintpath>..SQLiteX86System.Data.SQLite.DLL</hintpath>
<!--End Code-->

存檔後關閉,然後Reload此Project,選擇Platform (此時Reference的DLL路徑已經修正成你所選定的平台對應的DLL),Rebuild即可。

搜尋此網誌