SubmitChanges 不保存,但从更改集中删除插入,没有错误
我对 Linq to Sql SubmitChanges() 函数的调试功能有一个更深层次的问题。
我想在本地缓存数据库(localdbcache:服务器SqlExpress 2008客户端SqlCE)的表中保存一条记录。在调用 SubmitChanges 之前,我可以通过 DataContext.GetChangeSet() 找到新项目。调用 Submit Changes 后,要插入的项目已从 ChangeSet 中删除。 (这就是这个函数应该做的事情。) 数据库的日志输出中没有更改冲突,也没有错误。一点也不例外。 表的计数保持相同的值。
if ((e.Parameter == null) ||
(!e.Parameter.GetType().Equals(typeof(LibDB.Client.Vehicles))))
{
return;
}
LibDB.Client.Vehicles tmp = e.Parameter as LibDB.Client.Vehicles;
try
{
ChangeSet cs = this._dc.GetChangeSet();
if ((tmp == null) || (this._dc == null)) return;
if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 0)
this._dc.Vehicles.InsertOnSubmit(tmp);
else if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
this._dc.Vehicles.Attach(tmp, true);
else
return;
using (TransactionScope ts = new TransactionScope())
{
try
{
this._dc.SubmitChanges();
//this._dc.Refresh(RefreshMode.OverwriteCurrentValues,
// this._dc.Vehicles);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
MessageBox.Show("Vehicle not saved.");
this.vehSelector.ResetLayout();
}
我将不胜感激任何帮助,因为我失去了发现任何错误的希望, 预先感谢温斯顿
I have a deeper question regarding debug functionality of Linq to Sql SubmitChanges() Function.
I want to save a record in a table of a locally cached db (localdbcache: server SqlExpress 2008 client SqlCE). Before calling SubmitChanges I can find the new item via DataContext.GetChangeSet(). After calling Submit Changes, the items to insert have been removed from the ChangeSet. (That's what this function is supposed to do.)
There are no Changes Conflicts and no error in the db's log output. No Exception at all.
The table's Count stays at the same value.
if ((e.Parameter == null) ||
(!e.Parameter.GetType().Equals(typeof(LibDB.Client.Vehicles))))
{
return;
}
LibDB.Client.Vehicles tmp = e.Parameter as LibDB.Client.Vehicles;
try
{
ChangeSet cs = this._dc.GetChangeSet();
if ((tmp == null) || (this._dc == null)) return;
if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 0)
this._dc.Vehicles.InsertOnSubmit(tmp);
else if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
this._dc.Vehicles.Attach(tmp, true);
else
return;
using (TransactionScope ts = new TransactionScope())
{
try
{
this._dc.SubmitChanges();
//this._dc.Refresh(RefreshMode.OverwriteCurrentValues,
// this._dc.Vehicles);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
if (this._dc.Vehicles.Where(veh => veh.Vin == tmp.Vin).Count() == 1)
MessageBox.Show("Vehicle not saved.");
this.vehSelector.ResetLayout();
}
I would appreciate any help since I'm loosing hope to find any error,
Thanks in Advance Winston
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的实体在数据库中定义了主键吗?
Does your entity have a primary key defined in the db?
检查 _dc 新实例的计数。您应该在调用
.SaveChanges()
之后处理它。隔离问题的另一种方法是查看表本身的计数,而不使用 Linq。
Check the count on a new instance of _dc. You're supposed to dispose it after calling
.SaveChanges()
Another way to isolate the problem, is to take a look at the count on the table itself, without using Linq.