Parimal Raj
Parimal Raj

Reputation: 20575

Create table only if it does not exists for NHibernate with Sql Server Compact 3.5

I am trying to create a simple application that saves list of email/password in a table, to store data i am using Sql Server Compact 3.5

NHibernate is not creating table when using SchemaUpdate, as i have read it should have created table if not exists, but it is not creating the table, and i get a exception

Here is my NHibernate configuration

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();
    }
}

My entities

public class Email
{
    public virtual String Username { get; set; }
    public virtual String Password { get; set; }
    public virtual Guid Id { get; set; }
}

My Sample app :

static class Program
{
    [STAThread]
    static void Main()
    {

        using (var session = NHibernateHelper.OpenSession())
        {
            using (var trans = session.BeginTransaction())
            {
                var email1 = new Email() { Username = "[email protected]", Password = "raj", };
                session.Save(email1);

                trans.Commit();
            }
        }
        Console.Read();
    }
}

Upvotes: 2

Views: 2247

Answers (1)

yasmuru
yasmuru

Reputation: 1186

you can do this by checking the database file is already exists , if so based on the size of the database file you can assume that tables are created or not. Just see this following example you can get some idea .

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);
            }
        }
    }

Upvotes: 1

Related Questions