使用LINQ在主要自动增量键上插入记录?

时间:2016-12-12 03:15:04

标签: c# sql-server linq

在我搜索答案时,我遇到了这个问题How to insert a new record which has a auto increment number as primary key?,我希望能解决我的问题。但不幸的是,它没有,因为我在数据库中设置了主键。

我无法使用LINQ DataContext更新数据库,并且通过SubmitChanges更新不会引发错误,它只是不更新​​。我的理解是,我不需要在C#中创建自动生成的数字,因为数据库应该为此记录创建唯一的ID。

这是我用来尝试在自动增量主键列上更新数据库的代码。

Table newContext = new Table();

newContext.Staff_No = row.Staff_No;
newContext.Year_No = row.Year_No;
newContext.Month_No = row.Month_No;

dataContext.Tables.InsertOnSubmit(newContext);

try
{
   dataContext.SubmitChanges();
}
catch (Exception ex)
{
   Alerts.Error(@"Did not save", @"Error", ex);
}

上面的代码不起作用,但是当数据库列设置为主键但 NOT 自动增量时它会起作用。但我必须为ID列分配唯一的ID。 我只是将其添加到上面的代码和数据更新的非自动增量列,

newContext.ID = DateTime.UtcNow.Ticks;

我是否需要做些什么才能让它正确更新更改?

这是SQL SERVER中的设置

enter image description here

编辑:自动生成的类,

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Staff_Calc_TBL")]
    public partial class Staff_Calc_TBL : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private long _ID;

        private System.Nullable<int> _Staff_No;

        private System.Nullable<int> _Year_No;

        private System.Nullable<int> _Month_No;

        private System.Nullable<int> _Column_Index;

        private System.Nullable<decimal> _Column_Count_Value;

        private string _Column_DataVal;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIDChanging(long value);
    partial void OnIDChanged();
    partial void OnStaff_NoChanging(System.Nullable<int> value);
    partial void OnStaff_NoChanged();
    partial void OnYear_NoChanging(System.Nullable<int> value);
    partial void OnYear_NoChanged();
    partial void OnMonth_NoChanging(System.Nullable<int> value);
    partial void OnMonth_NoChanged();
    partial void OnColumn_IndexChanging(System.Nullable<int> value);
    partial void OnColumn_IndexChanged();
    partial void OnColumn_Count_ValueChanging(System.Nullable<decimal> value);
    partial void OnColumn_Count_ValueChanged();
    partial void OnColumn_DataValChanging(string value);
    partial void OnColumn_DataValChanged();
    #endregion

        public Staff_Calc_TBL()
        {
            OnCreated();
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
        public long ID
        {
            get
            {
                return this._ID;
            }
            set
            {
                if ((this._ID != value))
                {
                    this.OnIDChanging(value);
                    this.SendPropertyChanging();
                    this._ID = value;
                    this.SendPropertyChanged("ID");
                    this.OnIDChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Staff_No", DbType="Int")]
        public System.Nullable<int> Staff_No
        {
            get
            {
                return this._Staff_No;
            }
            set
            {
                if ((this._Staff_No != value))
                {
                    this.OnStaff_NoChanging(value);
                    this.SendPropertyChanging();
                    this._Staff_No = value;
                    this.SendPropertyChanged("Staff_No");
                    this.OnStaff_NoChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Year_No", DbType="Int")]
        public System.Nullable<int> Year_No
        {
            get
            {
                return this._Year_No;
            }
            set
            {
                if ((this._Year_No != value))
                {
                    this.OnYear_NoChanging(value);
                    this.SendPropertyChanging();
                    this._Year_No = value;
                    this.SendPropertyChanged("Year_No");
                    this.OnYear_NoChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Month_No", DbType="Int")]
        public System.Nullable<int> Month_No
        {
            get
            {
                return this._Month_No;
            }
            set
            {
                if ((this._Month_No != value))
                {
                    this.OnMonth_NoChanging(value);
                    this.SendPropertyChanging();
                    this._Month_No = value;
                    this.SendPropertyChanged("Month_No");
                    this.OnMonth_NoChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_Index", DbType="Int")]
        public System.Nullable<int> Column_Index
        {
            get
            {
                return this._Column_Index;
            }
            set
            {
                if ((this._Column_Index != value))
                {
                    this.OnColumn_IndexChanging(value);
                    this.SendPropertyChanging();
                    this._Column_Index = value;
                    this.SendPropertyChanged("Column_Index");
                    this.OnColumn_IndexChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_Count_Value", DbType="Decimal(4,2)")]
        public System.Nullable<decimal> Column_Count_Value
        {
            get
            {
                return this._Column_Count_Value;
            }
            set
            {
                if ((this._Column_Count_Value != value))
                {
                    this.OnColumn_Count_ValueChanging(value);
                    this.SendPropertyChanging();
                    this._Column_Count_Value = value;
                    this.SendPropertyChanged("Column_Count_Value");
                    this.OnColumn_Count_ValueChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_DataVal", DbType="NVarChar(50)")]
        public string Column_DataVal
        {
            get
            {
                return this._Column_DataVal;
            }
            set
            {
                if ((this._Column_DataVal != value))
                {
                    this.OnColumn_DataValChanging(value);
                    this.SendPropertyChanging();
                    this._Column_DataVal = value;
                    this.SendPropertyChanged("Column_DataVal");
                    this.OnColumn_DataValChanged();
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

0 个答案:

没有答案