繁体   English   中英

SubmitChanges不会更新,但是会插入新记录。 LINQ转SQL

[英]SubmitChanges not updating, but inserts new record. LINQ to SQL

我很难通过LINQ将数据更新到SQL,插入新记录效果很好。

代码正确地插入了新行并添加了主键,这是我要更新(更改数据库中已存在的值)该数据库未更新的那一行的问题,这是该问题的另一部分无法正常工作的代码。 这很奇怪,因为数据库已正确连接,并且DataContext插入了没有问题的新行,因此可以正常工作。 检查数据库可以确认这一点。

这是代码,

using System;
using System.Collections.Generic;
using System.Linq;

using Cost = Invoices.Tenant_Cost_TBL;

namespace Invoices
{   
    class CollectionGridEvents
    {
        static string conn = Settings.Default.Invoice_DbConnectionString;

        public static void CostDataGridCellEditing(DataGridRowEditEndingEventArgs e)
       {
        using (DatabaseDataContext DataContext = new DatabaseDataContext(conn))
           {
                var sDselectedRow = e.Row.Item as Cost;                
                if (sDselectedRow == null) return;
                if (sDselectedRow.ID == 0)
                {
                    sDselectedRow.ID = DateTime.UtcNow.Ticks;
                    DataContext.Tenant_Cost_TBLs.InsertOnSubmit(sDselectedRow);
                }
                else
                {
                    // these two lines are just for debuging
                    long lineToUpdateID = 636154619329526649; // this is the line to be updated primary key
                    long id = sDselectedRow.ID;     // this is to check the primary key on selected line is same

                    // these 3 lines are to ensure I am entering actual data into the DB
                    int? amount = sDselectedRow.Cost_Amount;
                    string name = sDselectedRow.Cost_Name;
                    int? quantity = sDselectedRow.Cost_Quantity;

                    sDselectedRow.Cost_Amount = amount;
                    sDselectedRow.Cost_Name = name;
                    sDselectedRow.Cost_Quantity = quantity;
                }
                try
                {                   
                    DataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Alert.Error("Did not save", "Error", ex);
                }
            }            
        }
    } 
}

我从这里调用方法,

private void CostDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        CollectionGridEvents.CostDataGridCellEditing(e);
    }

从数据库lineToUpdateID复制lineToUpdateID,并在那里检查当前选择的行主键是否相同,所以我知道我正在尝试更新同一行。

我在SO上浏览了许多相同类型的问题,例如这个Linq-to-Sql SubmitChanges没有更新字段……为什么? 但是仍然离发现问题出处还很近。

任何想法将不胜感激。

编辑: Cost只是using Cost = Invoices.Tenant_Cost_TBL;简称using Cost = Invoices.Tenant_Cost_TBL;

你不能这样做。 您需要将记录从数据库中取出,然后更新该记录。 然后将其保存回来。 像这样:

else
{
    // first get it
    var query =
        from ord in DataContext.Tenant_Cost_TBLs
        where ord.lineToUpdateID = 636154619329526649
        select ord;

    // then update it
    // Most likely you will have one record here
    foreach (Tenant_Cost_TBLs ord in query)
    {
        ord.Cost_Amount = sDselectedRow.Cost_Amount;
        // ... and the rest
        // Insert any additional changes to column values.
    }

}
try
{
    DataContext.SubmitChanges();
}
catch (Exception ex)
{
    Alert.Error("Did not save", "Error", ex);
}

是您可以遵循的示例。

或者,如果您不想首先选择,则可以使用直接查询。

DataContext.ExecuteCommand("update Tenant_Cost_TBLs set Cost_Amount =0 where ...", null);

您的对象(成本)未附加到数据库上下文。 您应该附加它,然后保存更改。 在这里检查解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM