I found another problem when I try to implement Save as new function.
After I create a new model from current model, if I use session object to populate a datatable like this
Code:
DataTable table = DBHelper.GetCodeDetails(session);
then try to insert new model
Code:
ITransaction tx = session.BeginTransaction();
session.Evict(currentModel);
session.Save(newModel);
tx.Commit();
NHibernate will update currentModel also.
If I remove
Code:
DataTable table = DBHelper.GetCodeDetails(session);
NHibernate only insert new model, do not update current model.
How can I fix it? Thanks!
This is code of DBHelper
Code:
using System;
using System.Collections;
using System.Data;
using System.Text;
using NHibernate;
namespace BugProject.Utilities
{
class DBHelper
{
public static DataTable GetCodeDetails(ISession Session)
{
DataTable table = null;
string sql = "";
sql = "SELECT M.Id, ";
sql += " M.Info ";
sql += "FROM Model AS M ";
try
{
IList codeDetails = Session.Find(sql);
table = GenerateDataTable(codeDetails);
}
catch (HibernateException ex)
{
throw ex;
}
return table;
}
private static DataTable GenerateDataTable(IList List)
{
DataTable table = null;
if (List != null)
{
table = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType, ColumnName
// and add to DataTable.
column = new DataColumn();
column.DataType = Type.GetType("System.Int32");
column.ColumnName = "Id";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Info";
table.Columns.Add(column);
IEnumerator e = List.GetEnumerator();
while (e.MoveNext())
{
object[] objs = (object[])e.Current;
row = table.NewRow();
row["Id"] = objs[0];
row["Info"] = objs[1];
table.Rows.Add(row);
}
}
return table;
}
}
}
You can try my
Source code