嵌套的工作单元和制作不需要的对象
本文关键字:不需要 对象 工作 单元 嵌套 | 更新日期: 2023-09-27 18:00:21
我的应用程序中有三个表单:frmTrucks、frmEditTruck和frmEditContent。frmTrucks在网格中显示我的卡车。我添加了一辆卡车,或者从网格中选择一辆卡车在frmEditTruck 中编辑
public void Edit()
{
using (NestedUnitOfWork nuow = session.BeginNestedUnitOfWork())
{
Truck currentTruck = nuow.GetNestedObject(
xpcTruck[gvTruck.GetDataSourceRowIndex(gvTruck.FocusedRowHandle)])
as Truck;
using (frmEditTruck form = new frmEditTruck(currentTruck))
{
if (form.ShowDialog() == DialogResult.OK)
nuow.CommitChanges();
}
}
}
在frmEditTruck中,有一些用于truck属性的文本框和两个按钮。btnSave和btAddContent。btnSave保存更改(现在.CommitChanges();)。btAdddContent的点击代码是:
Truck truck;
Session session;
public frmEditTruck(Truck truck)
{
InitializeComponent();
this.truck = truck;
this.session = truck.Session;
}
private void btnAddContent_Click(object sender, EventArgs e)
{
TContent content = new TContent(session);
using (frmEditTContent form = new frmEditTContent(content, truck))
{
if (form.ShowDialog() == DialogResult.OK)
truck.TContents.Add(content);
}
}
它显示frmEditContent。我可以将内容添加到我的卡车中。问题是当我按下AddContent然后取消它时。之后,当我按下frmEditTruck上的保存按钮时,它会在我的内容表中添加一个空行。我想解决这个问题。我该怎么修?我不确定我的问题对你来说是否足够清楚。请让我知道
public class Truck : XPObject
{
.
.
.
[Association("Truck-TContents")]
public XPCollection<TContent> TContents { get { return GetCollection<TContent>("TContents"); } }
}
public class TContent : XPObject
{
.
.
.
private Truck truck;
[Association("Truck-TContents")]
public Truck Truck
{
get
{
return truck;
}
set
{
SetPropertyValue("Truck", ref truck, value);
}
}
}
private void btnAddContent_Click(object sender, EventArgs e)
{
TContent content = new TContent(session);
using (frmEditTContent form = new frmEditTContent(content, truck))
{
if (form.ShowDialog() == DialogResult.OK)
truck.TContents.Add(content);
}
}
我已将代码更改为:
private void btnAddContent_Add(object sender, EventArgs e)
{
TContent content = new TContent(session);
using (frmEditTContent form = new frmEditTContent(content, truck))
{
if (form.ShowDialog() == DialogResult.OK)
{
truck.TContents.Add(content);
}
else
{
if (session.TrackingChanges)
session.RollbackTransaction();
}
}
}
它工作正常。