流利的Nhibernate的配置

时间:2018-08-15 07:15:46

标签: c# nhibernate fluent-nhibernate

我是NHibernate的新手。我可以使用Fluent Hibernate的以下语句进行处理吗,例如不存在表,如果该表存在,则创建新表,使用Fluent Nhibernate将其插入值中。

namespace ConsoleApplication1
{
   public class Program
    {

       public string connectionString = "Server=127.0.0.1; Port=5432; User Id=credit; Password=123;Database=databir;";
        public static ISessionFactory CreateSessionFactory()
        {
            ISessionFactory isessionFactory = Fluently.Configure()
                .Database(PostgreSQLConfiguration.PostgreSQL81
                    .ConnectionString("Server=127.0.0.1; Port=5432; User Id=credit; Password=123;Database=databir;"))


                   .Mappings(m => m
                        .FluentMappings.AddFromAssemblyOf<MapUser>()).ExposeConfiguration(c => {
var schema = new SchemaExport(c);
 schemaExport.Execute(true,false,false);

})


               .BuildSessionFactory();

            return isessionFactory;
        }
        static void Main(string[] args)
        {
            var staff = CreateSessionFactory();
            using (ISession session = staff.OpenSession())
            {
                using (var txt = session.BeginTransaction())
                {
                    user1 user = new user1
                    {

                        name = "jakhongir"
                    };
                    session.Save(user);
                    txt.Commit();
                }
            }


        }

    }
}

我不能每次都在数据库中创建新表,如果表不存在,那么每次插入该表时都要做什么以创建新表

1 个答案:

答案 0 :(得分:1)

此代码...

var schema = new SchemaExport(c);
schemaExport.Execute(true,false,false);

...每次创建SessionFactory时都会创建新表。您需要围绕此声明做出一些决策。 Updating模式而不是将其导出可能更合适?

相关问题