C# 用C语言更新NHibernate 3.3中的儿童
我一直在寻找如何更新父实体的子实体,但找不到符合我要求的内容。我正在使用数据网格视图控件将这些子数据绑定到表单和数据集。我需要能够知道数据网格中的哪些行被修改、删除和添加,这就是我需要将其传递给数据集的原因 下面是我的父映射和子映射: 父项:Fees.hbm.xml 子项:FeesLines.hbm.xmlC# 用C语言更新NHibernate 3.3中的儿童,c#,mysql,winforms,nhibernate,datagridview,C#,Mysql,Winforms,Nhibernate,Datagridview,我一直在寻找如何更新父实体的子实体,但找不到符合我要求的内容。我正在使用数据网格视图控件将这些子数据绑定到表单和数据集。我需要能够知道数据网格中的哪些行被修改、删除和添加,这就是我需要将其传递给数据集的原因 下面是我的父映射和子映射: 父项:Fees.hbm.xml 子项:FeesLines.hbm.xml 我需要能够使用NHibernate更新我的孩子记录,那么您是如何做到的?我找到了一种方法来更新我的孩子记录,下面是我如何做到的: public bool UpdateFees(string
我需要能够使用NHibernate更新我的孩子记录,那么您是如何做到的?我找到了一种方法来更新我的孩子记录,下面是我如何做到的:
public bool UpdateFees(string id, string code, string desc, DataSet details)
{
bool success = false;
using (ITransaction transaction = Session.BeginTransaction())
{
try
{
Fees fees = (Fees)Session.Get("EnrollmentSystem.Domain.Fees", Guid.Parse(id));
fees.Code = code;
fees.Description = desc;
if (details.HasChanges())
{
if (details.HasChanges(DataRowState.Added))
{
DataSet tempDataSet = details.GetChanges(DataRowState.Added);
foreach (DataRow row in tempDataSet.Tables[0].Rows)
{
FeesLines lines = new FeesLines();
lines.Fee = fees;
lines.Id = Guid.NewGuid();
lines.Description = row["Description"].ToString();
lines.Amount = (row["Amount"].ToString() == "") ? 0 : Convert.ToDecimal(row["Amount"].ToString());
fees.FeesDetails.Add(lines);
Session.Save(lines);
}
}
if (details.HasChanges(DataRowState.Modified))
{
DataSet editedSet = details.GetChanges(DataRowState.Modified);
foreach (DataRow row in editedSet.Tables[0].Rows)
{
var item = (from f in fees.FeesDetails
where f.Id.Equals(Guid.Parse(row["Id"].ToString()))
select f).SingleOrDefault();
if (item != null)
{
item.Description = row["Description"].ToString();
item.Amount = Convert.ToDecimal(row["Amount"].ToString());
Session.SaveOrUpdate(item);
}
}
}
if (details.HasChanges(DataRowState.Deleted))
{
DataSet deleted = details.GetChanges(DataRowState.Deleted);
for (int row = 0; row < deleted.Tables[0].Rows.Count; row++ )
{
var item = (from f in fees.FeesDetails
where f.Id.Equals(Guid.Parse(deleted.Tables[0].Rows[row][0, DataRowVersion.Original].ToString()))
select f).SingleOrDefault();
if (item != null)
{
fees.FeesDetails.Remove(item);
}
}
}
}
Session.SaveOrUpdate(fees);
transaction.Commit();
success = transaction.WasCommitted;
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
return success;
}
我创建了一个函数,在该函数中,我传递了父数据的新值,并传递了包含新的/修改的/删除的子数据集。:如果您有一个问题,您已经知道答案,并且希望公开记录该知识,以便包括您自己在内的其他人稍后可以找到它,完全可以问和回答你自己的问题…-但你不应该把这些都塞进问题箱。试着把你的问题重新写成一个问题,然后把你的答案加上去。哦,对不起,好的。可以!
<class name="FeesLines" table="fees_lines">
<id name="Id" column="id">
<generator class="guid" />
</id>
<property name="FeesId" column="fees_id" type="System.Guid" insert="false" update="false" />
<property name="Description" column="description" />
<property name="Amount" column="amount" />
<many-to-one name="Fee" class="Fees" column="fees_id" />
public bool UpdateFees(string id, string code, string desc, DataSet details)
{
bool success = false;
using (ITransaction transaction = Session.BeginTransaction())
{
try
{
Fees fees = (Fees)Session.Get("EnrollmentSystem.Domain.Fees", Guid.Parse(id));
fees.Code = code;
fees.Description = desc;
if (details.HasChanges())
{
if (details.HasChanges(DataRowState.Added))
{
DataSet tempDataSet = details.GetChanges(DataRowState.Added);
foreach (DataRow row in tempDataSet.Tables[0].Rows)
{
FeesLines lines = new FeesLines();
lines.Fee = fees;
lines.Id = Guid.NewGuid();
lines.Description = row["Description"].ToString();
lines.Amount = (row["Amount"].ToString() == "") ? 0 : Convert.ToDecimal(row["Amount"].ToString());
fees.FeesDetails.Add(lines);
Session.Save(lines);
}
}
if (details.HasChanges(DataRowState.Modified))
{
DataSet editedSet = details.GetChanges(DataRowState.Modified);
foreach (DataRow row in editedSet.Tables[0].Rows)
{
var item = (from f in fees.FeesDetails
where f.Id.Equals(Guid.Parse(row["Id"].ToString()))
select f).SingleOrDefault();
if (item != null)
{
item.Description = row["Description"].ToString();
item.Amount = Convert.ToDecimal(row["Amount"].ToString());
Session.SaveOrUpdate(item);
}
}
}
if (details.HasChanges(DataRowState.Deleted))
{
DataSet deleted = details.GetChanges(DataRowState.Deleted);
for (int row = 0; row < deleted.Tables[0].Rows.Count; row++ )
{
var item = (from f in fees.FeesDetails
where f.Id.Equals(Guid.Parse(deleted.Tables[0].Rows[row][0, DataRowVersion.Original].ToString()))
select f).SingleOrDefault();
if (item != null)
{
fees.FeesDetails.Remove(item);
}
}
}
}
Session.SaveOrUpdate(fees);
transaction.Commit();
success = transaction.WasCommitted;
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
return success;
}