0

关于 Linq to Sql SubmitChanges() 函数的调试功能,我有一个更深层次的问题。

我想将记录保存在本地缓存数据库的表中(localdbcache:服务器 SqlExpress 2008 客户端 SqlCE)。在调用 SubmitChanges 之前,我可以通过 DataContext.GetChangeSet() 找到新项目。调用 Submit Changes 后,要插入的项目已从 ChangeSet 中删除。(这就是这个函数应该做的。)数据库的日志输出中没有更改冲突,也没有错误。一点也不例外。表的 Count 保持相同的值。

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();
}

我会很感激任何帮助,因为我失去了找到任何错误的希望,在此先感谢温斯顿

4

2 回答 2

0

您的实体是否在数据库中定义了主键?

于 2010-04-01T13:19:47.620 回答
0

检查 _dc 的新实例的计数。你应该在打电话后处理它.SaveChanges()

隔离问题的另一种方法是查看表本身的计数,而不使用 Linq。

于 2010-04-01T13:24:32.813 回答