向数据库提交更改不工作

本文关键字:工作 数据库 提交 | 更新日期: 2023-09-27 18:18:50

我有以下内容:

    var db = new datesDataContext();
    var query =
    from ord in db.Dates
    where ord.id == id
    select ord;
    foreach (Date ord in query)
    {
        ord.date1 = product.date1;
        ord.name = product.name;
    }
    db.SubmitChanges();

一切运行正常(没有错误等),除了SubmitChanges没有在数据库中进行更改。

奥德。Dat1和order .name肯定被设置了…

编辑:这是我的日期类(它说部分,但它是整个类,没有其他定义):

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Dates")]
public partial class Date
{
    private System.Nullable<int> _id;
    private System.Nullable<System.DateTime> _date1 = DateTime.Now;
    private string _name;
    public Date()
    {
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int")]
    public System.Nullable<int> id
    {
        get
        {
            return this._id;
        }
        set
        {
            if ((this._id != value))
            {
                this._id = value;
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Name="date", Storage="_date1", DbType="DateTime")]
    public System.Nullable<System.DateTime> date1
    {
        get
        {
            return this._date1;
        }
        set
        {
            if ((this._date1 != value))
            {
                this._date1 = value;
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_name", DbType="Text", UpdateCheck=UpdateCheck.Never)]
    public string name
    {
        get
        {
            return this._name;
        }
        set
        {
            if ((this._name != value))
            {
                this._name = value;
            }
        }
    }
}

向数据库提交更改不工作

是你的日期类实现INotifyPropertyChanged?

编辑:在你的Date类中实现INotifyPropertyChanged
public partial class Date : INotifyPropertyChanged
{
         public event PropertyChangedEventHandler PropertyChanged;
         private void NotifyPropertyChanged(String info)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

…类定义的其余部分

和在每个setter方法中更改值后使用此代码

NotifyPropertyChanged("PropertyName");

PropertyName在你的例子中是id, date1和name。

您还需要指定主键,否则跟踪更改将无法工作。

应该可以。您的配置或模型类一定有问题。还可以包括Date类中的代码吗?

我在你的"name"属性旁边看到这个:UpdateCheck=UpdateCheck。从来没有

我猜这个设置导致属性不更新