SubmitChanges到数据库不起作用

时间:2011-12-25 04:41:40

标签: c# asp.net linq asp.net-mvc-3

我有以下内容:

    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没有在数据库中进行更改外,一切运行正常(没有错误等)。

ord.dat1和ord.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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的Date类是否实现了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。

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

答案 1 :(得分:1)

这应该有效。您的配置或模型类都必须出错。你也可以包含Date类的代码吗?

我在你的“name”属性旁边看到了这个:UpdateCheck = UpdateCheck.Never

我猜这个设置导致该属性无法更新。

相关问题