C# 仅当使用Sql Server Compact 3.5的NHibernate不存在表时才创建表
我正在尝试创建一个简单的应用程序,将电子邮件/密码列表保存在一个表中,以存储我使用的Sql Server Compact 3.5的数据 当使用C# 仅当使用Sql Server Compact 3.5的NHibernate不存在表时才创建表,c#,.net,nhibernate,fluent-nhibernate,sql-server-ce,C#,.net,Nhibernate,Fluent Nhibernate,Sql Server Ce,我正在尝试创建一个简单的应用程序,将电子邮件/密码列表保存在一个表中,以存储我使用的Sql Server Compact 3.5的数据 当使用SchemaUpdate时,NHibernate没有创建表,因为我已经读到,如果不存在,它应该创建表,但它没有创建表,并且我得到一个异常 这是我的NHibernate配置 public class NHibernateHelper { private static ISessionFactory sessionFactory; priva
SchemaUpdate
时,NHibernate没有创建表,因为我已经读到,如果不存在,它应该创建表,但它没有创建表,并且我得到一个异常
这是我的NHibernate配置
public class NHibernateHelper
{
private static ISessionFactory sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
InitializeSessionFactory();
return sessionFactory;
}
}
private static void InitializeSessionFactory()
{
sessionFactory =
Fluently.Configure()
.Database
(
MsSqlCeConfiguration.Standard
.ConnectionString(@"Data Source=E:\tumblr_db.sdf").ShowSql()
)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Email>()
)
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
我的示例应用程序:
static class Program
{
[STAThread]
static void Main()
{
using (var session = NHibernateHelper.OpenSession())
{
using (var trans = session.BeginTransaction())
{
var email1 = new Email() { Username = "test@example.com", Password = "raj", };
session.Save(email1);
trans.Commit();
}
}
Console.Read();
}
}
您可以通过检查数据库文件是否已经存在来实现这一点,如果已经存在,则根据数据库文件的大小,您可以假定表是否已创建。只要看看下面这个例子,你就会有一些想法
String DbFile = "E:\tumblr_db.sdf";
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard
.UsingFile(DbFile)
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Form1>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
if (!File.Exists(DbFile))
{
new SchemaExport(config)
.Create(false, true);
}
else
{
FileInfo info = new FileInfo(DbFile);
long size = info.Length;
if (size == 0)
{
new SchemaExport(config).Create(false, true);
}
}
}
String DbFile=“E:\tumblr\u db.sdf”;
私有静态ISessionFactory CreateSessionFactory()
{
流畅地返回。Configure()
.数据库(
SQLiteConfiguration.Standard
.UsingFile(DbFile)
)
.Mappings(m=>
m、 FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(构建架构)
.BuildSessionFactory();
}
私有静态void BuildSchema(配置)
{
如果(!File.Exists(DbFile))
{
新SchemaExport(配置)
.创建(假、真);
}
其他的
{
FileInfo=newfileinfo(DbFile);
长尺寸=信息长度;
如果(大小==0)
{
新建SchemaExport(config).Create(false,true);
}
}
}
看起来像是黑客。我认为OP正在寻找一种解决方案,其中NHibernate检查每个表(甚至列)是否存在,并在必要时创建它。
String DbFile = "E:\tumblr_db.sdf";
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
SQLiteConfiguration.Standard
.UsingFile(DbFile)
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Form1>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
if (!File.Exists(DbFile))
{
new SchemaExport(config)
.Create(false, true);
}
else
{
FileInfo info = new FileInfo(DbFile);
long size = info.Length;
if (size == 0)
{
new SchemaExport(config).Create(false, true);
}
}
}