7
我一直在關注如何在SQLite中使用NHibernate,其中大部分都與單元測試數據庫CRUD操作有關。所以,我迄今爲止搜索並遵循的示例都與此相關。這很好,但問題是每次運行我的程序時都會重新創建數據庫!我如何修改我的代碼,以便如果數據庫已經存在NHibernate不會創建它?是的,我試圖檢查File.Exists,但它被忽略;我相信因爲NHibernate首先獲取文件。用NHibernate創建一個SQLite數據庫,但只有一次
這是我的映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="show_sql">false</property>
</session-factory>
</hibernate-configuration>
而且我全碼:
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using PruebaNHLite.Domain;
namespace PruebaNHLite
{
public class Program
{
public static ISession sess;
public static Configuration cfg;
public static SQLiteConnection connection;
private const string CONNECTION_STRING =
@"Data Source=nhlite.db;Pooling=true;FailIfMissing=false;
BinaryGUID=false;New=false;Compress=true;Version=3";
static void Main(string[] args)
{
Init();
BuildSchema();
Insert();
Retrieve();
sess.Close();
sess = null;
}
public static void Init()
{
// Initialize NHibernate
cfg = new Configuration();
cfg.Configure();
IDictionary<string, string> props = new Dictionary<string, string>();
props.Add("connection.connection_string", CONNECTION_STRING);
props.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
props.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
props.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
props.Add("query.substitutions", "true=1;false=0");
props.Add("show_sql", "false");
cfg.SetProperties(props);
cfg.AddAssembly(typeof(Person).Assembly);
connection = new SQLiteConnection(CONNECTION_STRING);
connection.Open();
// Get ourselves an NHibernate Session
var sessions = cfg.BuildSessionFactory();
sess = sessions.OpenSession();
}
private static void BuildSchema()
{
NHibernate.Tool.hbm2ddl.SchemaExport schemaExport
= new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
schemaExport.Execute(false, true, false, connection, null);
}
public static void Insert()
{
// Create a Person...
var person = new Person
{
Name = "Almudena",
Surname = "Pamplinas",
Age = 5
};
// And save it to the database
sess.Save(person);
sess.Flush();
}
public static void Retrieve()
{
IQuery q = sess.CreateQuery("FROM Person");
foreach (var p in q.List().Cast<Person>())
{
Console.WriteLine(string.Format("{0} {1}, de {2} años.",
p.Name, p.Surname, p.Age));
}
Console.ReadLine();
}
}
}
謝謝你,瓦迪姆,這就像一個魅力。你能否詳細說明你的謹慎?我正在做的這些測試是爲了學習我想用SQLite和NHibernate構建的桌面程序,並且我需要從模式中自動創建SQLite數據庫。有沒有一種生產就緒的方式來做到這一點? – CMPerez 2012-04-27 06:25:24
我得到的是,我不會依賴我的應用程序/ nhibernate在部署時創建數據庫和模式。我會手動創建數據庫和模式,然後部署應用程序。 – Vadim 2012-04-27 15:02:00