I'm new in NHibernate. Can I do with schemas of Fluent Hibernate following statement like if table dosn't exist create new table if this table is existed insert into it value using 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();
}
}
}
}
}
I can't insert into database it's creating new table of each of time what to do in order to insert into table if table dosn't exist just create it with this value
This code...
var schema = new SchemaExport(c);
schemaExport.Execute(true,false,false);
...will create new tables every time you create a SessionFactory
. You need some decision making around this statement. Updating the schema rather than exporting it might be more appropriate?